Generat Random Number between 1 and 13

A

Andrew Banks

How can I generate random number between 1 and 13 inclusive.

I''m doing this at the moment by calling the following

private int PickCard()
{
Random RandomGenerator = new Random();
int RandomNumber = new int();
RandomNumber = RandomGenerator.Next(1, 14);
return RandomNumber;
}

But if I call it a few times straight after each other I get the same
numbers returned.
 
M

Marina

Don't create a new random number generator each time. Create one up front,
and just use it over and over.
 
J

Jon Skeet [C# MVP]

Andrew Banks said:
How can I generate random number between 1 and 13 inclusive.

I''m doing this at the moment by calling the following

private int PickCard()
{
Random RandomGenerator = new Random();
int RandomNumber = new int();
RandomNumber = RandomGenerator.Next(1, 14);
return RandomNumber;
}

But if I call it a few times straight after each other I get the same
numbers returned.

That's because you're creating a new random number generator each time.
Create one, once, store it in a static variable, and then always use
the same one.
 
A

Andrew Banks

Jon,

Could you provide some sample code for this?

I understandwhat you are saying and can see the problem but beeing new to
..NET I'm not sure how to apply what you are suggesting.

Thanks,
Andrew
 
M

Marina

I am not sure if a static variable is the way to go, since it would be
shared across requests and threads, and two users might try using it at the
same time.

I would just suggest moving the line :
Random RandomGenerator = new Random();

outside your method, making it a class level variable. Then, just keep
using the RandomGenerator variable as you were before.

If you wanted to make it static, you would put the 'static' keyword in front
of it.


Andrew Banks said:
Jon,

Could you provide some sample code for this?

I understandwhat you are saying and can see the problem but beeing new to
.NET I'm not sure how to apply what you are suggesting.

Thanks,
Andrew
 
J

Jon Skeet [C# MVP]

Marina said:
I am not sure if a static variable is the way to go, since it would be
shared across requests and threads, and two users might try using it at the
same time.

So put a lock round it. Shouldn't cause any problems.
I would just suggest moving the line :
Random RandomGenerator = new Random();

outside your method, making it a class level variable. Then, just keep
using the RandomGenerator variable as you were before.

If you wanted to make it static, you would put the 'static' keyword in front
of it.

Making it an instance method would indeed solve some of the problem -
but then two requests which came in at the same time could get the same
number, which could cause security concerns.
 
J

Jon Skeet [C# MVP]

Andrew Banks said:
Could you provide some sample code for this?

I understandwhat you are saying and can see the problem but beeing new to
.NET I'm not sure how to apply what you are suggesting.

Change your code to:

static Random randomNumberGenerator = new Random();

int PickCard()
{
lock (randomNumberGenerator)
{
return randomNumberGenerator.Next(1, 14);
}
}
 
G

George Ter-Saakov

Random is using seed value to start generate the random numbers.
If seed value is the same then Random will generate the same sequence.

By default the empty constructor using current datetime as a seed value.
So if you are making your calls very fast. Then the seed value is the same.

The solution is pretty much was given to you.
Make RandomGenerator - global/static and then synchronize access to it from
different threads.

George.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top