3

I usually generate random stuff in the following manner:

Random random = new Random(DateTime.Now.Millisecond);

for (int i = 0; i < 100; i++)
{
    Console.WriteLine(random.Next(0, 100));
}

I was wondering whether there is a difference if I put the Random instantiation within the loop:

for (int i = 0; i < 100; i++)
{
    Random random = new Random(DateTime.Now.Millisecond);

    Console.WriteLine(random.Next(0, 100));
}

Which is more random or they are the same?

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
Kiril Stanoev
  • 1,865
  • 2
  • 20
  • 33
  • One of a great many questions on how to use Random - for instance, http://stackoverflow.com/questions/2727538/random-encounter-not-so-random or http://stackoverflow.com/questions/3079996/random-numbers-in-c-sharp - which cover very similar ground. – AAT Nov 03 '11 at 10:36

7 Answers7

5

The first (i.e. outside the loop) is more efficient AND more random since the second creates lots of Random instances in very short time which will lead to several instances having the same seed (i.e. same Millisecond) which in turn means generating the same random numbers over and over again.

From MSDN

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated.

Yahia
  • 69,653
  • 9
  • 115
  • 144
1

Outside of the loop, based on the samples in this documentation:
http://msdn.microsoft.com/en-us/library/system.random.aspx

Meligy
  • 35,654
  • 11
  • 85
  • 109
  • loved this line *By default, the parameterless constructor of the Random class uses the system clock to generate its seed value* – V4Vendetta Nov 03 '11 at 09:34
0

The first is more random, the second one may end up writing the same value a lot of times (as many as there are iterations in the same millisecond).

"Random" is not truly random, it uses an algorithm to calculate the next value from the previous one. The starting value (the 'seed') is just the first value in the sequence. So the same starting value results in the same sequence.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0

You should reuse the Random variable. In your second example you'll often get the same result.

George Duckett
  • 31,770
  • 9
  • 95
  • 162
0

The later is producing more random number. According to MSDN the value that you providing (Millisecond) is used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used.

So, Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

If your application requires different random number sequences, invoke this constructor repeatedly with different seed values.

More...

0

i would suggest you to use first one beacuase your both immplementations are more less same but the second one could creates colliosions (same keys).

0

Second way can create the same ouputs.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91