Two different seeds producing the same "random" sequence

Posted by Ruud Lenders on Stack Overflow See other posts from Stack Overflow or by Ruud Lenders
Published on 2012-11-13T16:16:55Z Indexed on 2012/11/13 17:01 UTC
Read the original article Hit count: 352

Filed under:
|
|
|
|

Maybe there is a very logic explanation for this, but I just can't seem to understand why the seeds 0 and 2,147,483,647 produce the same "random" sequence, using .NET's Random Class (System).

Quick code example:

ushort len = 8;
Random r0 = new Random(0), r1 = new Random(1), r2 = new Random(int.MaxValue); //2,147,483,647
byte[] b0 = new byte[len], b1 = new byte[len], b2 = new byte[len];

r0.NextBytes(b0);
r1.NextBytes(b1);
r2.NextBytes(b2);

for (int i = 0; i < len; i++)
{
  System.Diagnostics.Debug.WriteLine("{0}\t\t{1}\t\t{2}", b0[i], b1[i], b2[i]);
}

Console.ReadLine();

Output:

26      70      26
12      208     12
70      134     76
111     130     111
93      64      93
117     151     115
228     228     228
216     163     216

As you can see, the first and the third sequence are the same. Can someone please explain this to me?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET