我有一个 C# 类,RandomVariableUniforme;该类有如下方法:(Max和Min是修改字段max和min的属性)
public double runif(ref Random randomNumber)
{
double number;
number = Min + randomNumber.NextDouble() * (Max - Min);
Console.Write("number = {0,6:F3} ", number);
return number;
}
在主文件中,我有以下内容:(其中 A[i] 和 A[j] 是上述类的对象,具有不同的最大值和最小值,保存在数组中。)
int n = 5;
double[] x = new double[n];
double[] y = new double[n];
Random randomNumber = new Random();
for (int cont = 0; cont < n; cont++)
{
x[cont] = A[i].runif(ref randomNumber);
y[cont] = A[j].runif(ref randomNumber);
Console.WriteLine("\nx[cont] = {0,6:F3} y[cont] = {0,6:F3}", x[cont], y[cont]);
}//end for
然而,当该方法正确生成伪随机数时,程序只保存第一次调用。输出如下:(其中 number 是该方法生成的数字和应该由下面的相应向量保存的数字
number = 509,986 number = 470,079
x[cont] = 509,986 y[cont] = 509,986
number = 507,614 number = 418,733
x[cont] = 507,614 y[cont] = 507,614
number = 432,070 number = 463,316
x[cont] = 432,070 y[cont] = 432,070
number = 446,716 number = 476,604
x[cont] = 446,716 y[cont] = 446,716
number = 440,344 number = 459,617
x[cont] = 440,344 y[cont] = 440,344
任何想法为什么会发生这种情况?
提前致谢。