I checked source code of Random class of .Net here. What surprises me is the last line
public Random(int Seed) {
int ii;
int mj, mk;
//Initialize our Seed array.
//This algorithm comes from Numerical Recipes in C (2nd Ed.)
int subtraction = (Seed == Int32.MinValue) ? Int32.MaxValue : Math.Abs(Seed);
mj = MSEED - subtraction;
SeedArray[55]=mj;
mk=1;
for (int i=1; i<55; i++) { //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
ii = (21*i)%55;
SeedArray[ii]=mk;
mk = mj - mk;
if (mk<0) mk+=MBIG;
mj=SeedArray[ii];
}
for (int k=1; k<5; k++) {
for (int i=1; i<56; i++) {
SeedArray[i] -= SeedArray[1+(i+30)%55];
if (SeedArray[i]<0) SeedArray[i]+=MBIG;
}
}
inext=0;
inextp = 21;
Seed = 1;
}
What is the purpose of assigning parameter in the end of the method?
Since the Seed
parameter is not being passed by ref
and int
is a value type, then the last line has no effect whatsoever.
Thanks to @Alex K for pointing that out, after checking the algorithm they adapted in Numerical Recipes in C, indeed they copied and pasted that last line:
if ( * idum < 0 || iff == 0) {
Initialization.
iff = 1;
mj = labs(MSEED - labs( * idum));
Initialize ma[55] using the seed idum and the
mj %= MBIG;
large number MSEED.
ma[55] = mj;
mk = 1;
for (i = 1; i <= 54; i++) {
ii = (21 * i) % 55;
ma[ii] = mk;
mk = mj - mk;
if (mk < MZ) mk += MBIG;
mj = ma[ii];
}
for (k = 1; k <= 4; k++)
(i = 1; i <= 55; i++) {
ator.”
ma[i] -= ma[1 + (i + 30) % 55];
if (ma[i] < MZ) ma[i] += MBIG;
}
inext = 0;
inextp = 31;
* idum = 1;
}