【发布时间】:2017-11-25 17:03:15
【问题描述】:
我检查了 .Net here 的 Random 类的源代码。让我吃惊的是最后一行
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;
}
在方法的最后赋值参数的目的是什么?
【问题讨论】:
-
为什么不呢?我不知道他们为什么选择这样做,但我也看不出为什么不这样做。
-
@LasseV.Karlsen 为什么不呢?因为它什么都不做。
-
我现在看到了,我错误地认为它是一个正在设置的属性,我现在看到它是参数。
-
确实,套管让你离开这里,但它真的没有用。猜猜这是一个实习生写的代码;)
-
//这个算法来自Numerical Recipes in C (2nd Ed.) - 也许最后一行是
*seed = 1;- 我检查过 - 它是 - 它是一个下降在副本中进行了一些调整。
标签: c# random assignment-operator redundancy