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:
var random1 = new Random(0);
var random2 = new Random(1);
var random3 = new Random(int.MaxValue); //2,147,483,647
var buffer1 = new byte[8];
var buffer2 = new byte[8];
var buffer3 = new byte[8];
random1.NextBytes(buffer1);
random2.NextBytes(buffer2);
random3.NextBytes(buffer3);
for (int i = 0; i < 8; i++)
{
Console.WriteLine("{0}\t\t{1}\t\t{2}", buffer1[i], buffer2[i], buffer3[i]);
}
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?
EDIT: Apparently, as alro pointed out, these sequences are not the same. But they are very similar.
The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.
❓ What is a Random Seed? A random seed is used to ensure that results are reproducible. In other words, using this parameter makes sure that anyone who re-runs your code will get the exact same outputs. Reproducibility is an extremely important concept in data science and other fields.
A random seed is a starting point in generating random numbers. A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001).
Seed function is used to save the state of a random function, so that it can generate same random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value). The seed value is the previous value number generated by the generator.
Well, the reason will be connected with whatever derivation function is used by the Random class to derive a pseudo-random sequence from the seed. The real answer, therefore, is mathematical (and beyond my ability).
Indeed - I don't believe there's any guarantee that two different seeds will necessarily produce different sequences anyway.
Edit Okay - I'm going to do what bitbonk has done - but explain why:
public Random(int Seed)
{
int num = (Seed == -2147483648) ? 2147483647 : Math.Abs(Seed);
int num2 = 161803398 - num;
this.SeedArray[55] = num2;
int num3 = 1;
for (int i = 1; i < 55; i++)
{
int num4 = 21 * i % 55;
this.SeedArray[num4] = num3;
num3 = num2 - num3;
if (num3 < 0)
{
num3 += 2147483647;
}
num2 = this.SeedArray[num4];
}
for (int j = 1; j < 5; j++)
{
for (int k = 1; k < 56; k++)
{
this.SeedArray[k] -= this.SeedArray[1 + (k + 30) % 55];
if (this.SeedArray[k] < 0)
{
this.SeedArray[k] += 2147483647;
}
}
}
this.inext = 0;
this.inextp = 21;
Seed = 1;
}
We don't actually need to go too far into the code to see why - reading the code from top to bottom these are the values that will be stored by the above code when the seed is 0
and when the seed is 2147483647
:
int num = (Seed == -2147483648) ? 2147483647 : Math.Abs(Seed);
=> num is 0 and 2147483647
int num2 = 161803398 - num;
=> num2 is 161803398 and -1985680249
this.SeedArray[55] = num2;
=> this.SeedArray is as above in both cases
int num3 = 1;
for (int i = 1; i < 55; i++)
{
int num4 = 21 * i % 55
this.SeedArray[num4] = num3;
=> num4 is 21, SeedArray[21] is 1
num3 = num2 - num3
=> num3 is 161803397 and -1985680250
if(num3 < 0)
num3 += 2147483647
=> num3 is 161803397 and 161803397
After just the very first loop, algorithm has already converged for the two seed values.
Edit
As has been pointed out on the question - the sequences aren't the same - but they are clearly very very similar - and here we can see the reason for that similarity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With