I am migrating a method that is used for decoding from .NET Framework 1.1 to .NET Framework 4. I noticed that implementation of Random changed. So given the same seed, Random.NextBytes returns different result.
So if I run the following code.
byte[] bytes = new byte[4];
System.Random random = new System.Random(50);
random.NextBytes(bytes);
for(int i=0; i< bytes.Length; i++)
{
Console.WriteLine("bytes[" + i + "] = " + bytes[i]);
}
Under .NET Framework 1.1 it returns:
bytes[0] = 216
bytes[1] = 124
bytes[2] = 183
bytes[3] = 58
Under .NET framework 4 it returns:
bytes[0] = 154
bytes[1] = 49
bytes[2] = 183
bytes[3] = 48
What is the best way to resolve this problem?
Random
generates random numbers is not documented and, therefore, may change between versions. UsingRandom
for version-independent number sequences is a bug. Period. – CatholiconRandom
class explicitly provides "repeatable sequences of psuedo-random numbers". If that's what your program needs, then having the .NET framework change the sequence across versions is a bug, regardless of the reason why you need it. – Monophysite