0

I know there are lots of questions about random in here but they didn't help me at all, they are about generating same numbers in a single runtime, and mine isn't

So here is my code.

    Random rnd = new Random(0);
    for (int c = 0; c < arraySize; c++)
        data[c] = rnd.Next();

if i run it for the first time.
the element of rnd.Next() would be 1559595546 next output would be 1755192844 and so on,
if i close the program and re run it,
the same set of numbers would be generated

    data[0] = 1559595546
    data[1] = 1755192844   
and so on,

Why is that so? isnt it should generate different set of numbers every time i close the program and run it?

please help me understand this.

Albert Laure
  • 1,702
  • 5
  • 20
  • 49
  • possible duplicate of [RandomNumber method returns same number every time called](http://stackoverflow.com/questions/1004929/randomnumber-method-returns-same-number-every-time-called) – Alexei Levenkov Mar 17 '14 at 02:16

2 Answers2

7

No, you're providing a fixed seed of 0. Therefore the output is always the same. Take a read of the documentation for the constructor overload that you are using:

http://msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx

paying particular attention to the following statement:

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

Perhaps you should:

 var rnd = new Random(); //no seed
spender
  • 117,338
  • 33
  • 229
  • 351
  • Thanks mate, now i understand the use of that Seed. i'll accept it as soon as i can. – Albert Laure Mar 17 '14 at 01:51
  • +1. Can't find correct duplicate :)... @User6675636b20796f7521 - don't forget to check top answer for similar [random generates same number](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) to avoid the second trap with recreating `Random` in the loop too. – Alexei Levenkov Mar 17 '14 at 02:19
0

Thsi is because you have provide Seed 0 like

Random = new Random(0); //0 is Seed

If you remove it then it will generate different random numbers as you required.

byteboy
  • 107
  • 1
  • 10