Possible Duplicate:
Random number generator only generating one random number
A beginner question. I have a very simple program that draws a line and I want to randomize the locations, but each time I create a new instance of Random it returns the same value. Where is the problem? Thank you.
private void Draw()
{
Random random1 = new Random();
int randomNumber1 = random1.Next(0, 300);
Random random2 = new Random();
int randomNumber2 = random2.Next(0, 300);
Random random3 = new Random();
int randomNumber3 = random3.Next(0, 300);
Random random4 = new Random();
int randomNumber4 = random4.Next(0, 300);
System.Drawing.Graphics g = this.CreateGraphics();
Pen green = new Pen(Color.Green, 5);
g.DrawLine(green, new Point(randomNumber1, randomNumber2),
new Point(randomNumber3, randomNumber4));
}
private void btndraw1_Click(object sender, EventArgs e)
{
Draw();
}
What random class of .Net needs is a seed value you can use a date value as a seed and it would work.
A random number generator (RNG) does not actually generate random numbers. Instead, it uses an algorithm to define a sequence of numbers, that appear to be random. This sequence depends on the
seed
that is run through said algorithm at the time you RNG is created.By default, RNGs are created using the system's clock as a seed, since the clock will usually vary every time the program is run, making it extremely difficult to predict the "random" sequence.
In your case, it is very likely, that the clock didn't change between creating a random object and another; possibly due to CPU-internal re-ordering of instructions.
As Blachshma states, it is best to create only a single random object and use only that.
Keep in mind that any instance of
System.Random
are not guaranteed to be thread-safe, meaning that if you plan on having multiple threads share the same random object, you must lock it.Failure to do so might break your random object, leading to consequent calls returning only 0 as a result.
You shouldn't create a new
Random
object for each number. Instead, use the same object:The reason this happens is that every time you do a new
Random
it is initialized using the clock. So in a tight loop (or many calls one after the other) you get the same value lots of times since all those random variables are initialized with the same seed.To solve this: Create only one Random variable, preferably outside your function and use only that one instance.
Simply use the same instance:
Random numbers in programming are not really random; they are based on some unique seed that is taken and manipulated to generate what appears to be set of random numbers. Using the same seed will result in same set of numbers.
The default constructor of the
Random
class is using the number of milliseconds elapsed since the system started as the seed, so what actually happened is the same seed was used.There is really no reason to create more than once
Random
instance; the single instance will generate random set of numbers on each execution of the code.To prove my above statement of default seed, I used reflection:
And the
Environment.TickCount
: