C#产生的随机数,默认情况下,是[0,1]上的均匀分布吗?
发布网友
发布时间:2024-01-24 03:38
我来回答
共4个回答
热心网友
时间:2024-01-24 06:46
//Random 有NextDouble()和Next()等多个方法,
//NextDouble()方法生成[0,1]上的随机数
//它不是绝对的均匀分布。
Random ran = new Random();
for(int i = 0 ;i<100;i++)
{
Console.WriteLine(ran.NextDouble());
}
热心网友
时间:2024-01-24 06:47
Random.Next generates a random number whose value ranges from zero to less than Int32.MaxValue. To generate a random number whose value ranges from zero to some other positive number, use the Random.Next(Int32) method overload. To generate a random number within a different range, use the Random.Next(Int32, Int32) method overload.
实现代码:
//seed:Gets the number of milliseconds elapsed since the system started.
public Random(int Seed)
{
this.SeedArray = new int[0x38];
int num2 = 0x9a4ec86 - Math.Abs(Seed);
this.SeedArray[0x37] = num2;
int num3 = 1;
for (int i = 1; i < 0x37; i++)
{
int index = (0x15 * i) % 0x37;
this.SeedArray[index] = num3;
num3 = num2 - num3;
if (num3 < 0)
{
num3 += 0x7fffffff;
}
num2 = this.SeedArray[index];
}
for (int j = 1; j < 5; j++)
{
for (int k = 1; k < 0x38; k++)
{
this.SeedArray[k] -= this.SeedArray[1 + ((k + 30) % 0x37)];
if (this.SeedArray[k] < 0)
{
this.SeedArray[k] += 0x7fffffff;
}
}
}
this.inext = 0;
this.inextp = 0x15;
Seed = 1;
}
public virtual int Next()
{
return this.InternalSample();
}
private int InternalSample()
{
int inext = this.inext;
int inextp = this.inextp;
if (++inext >= 0x38)
{
inext = 1;
}
if (++inextp >= 0x38)
{
inextp = 1;
}
int num = this.SeedArray[inext] - this.SeedArray[inextp];
if (num < 0)
{
num += 0x7fffffff;
}
this.SeedArray[inext] = num;
this.inext = inext;
this.inextp = inextp;
return num;
}
热心网友
时间:2024-01-24 06:47
//Random 有NextDouble()和Next()等多个方法,
//NextDouble()方法生成[0,1]上的随机数
//它不是绝对的均匀分布。
Random ran = new Random();
for(int i = 0 ;i<100;i++)
{
Console.WriteLine(ran.NextDouble());
}
热心网友
时间:2024-01-24 06:47
Random.Next generates a random number whose value ranges from zero to less than Int32.MaxValue. To generate a random number whose value ranges from zero to some other positive number, use the Random.Next(Int32) method overload. To generate a random number within a different range, use the Random.Next(Int32, Int32) method overload.
实现代码:
//seed:Gets the number of milliseconds elapsed since the system started.
public Random(int Seed)
{
this.SeedArray = new int[0x38];
int num2 = 0x9a4ec86 - Math.Abs(Seed);
this.SeedArray[0x37] = num2;
int num3 = 1;
for (int i = 1; i < 0x37; i++)
{
int index = (0x15 * i) % 0x37;
this.SeedArray[index] = num3;
num3 = num2 - num3;
if (num3 < 0)
{
num3 += 0x7fffffff;
}
num2 = this.SeedArray[index];
}
for (int j = 1; j < 5; j++)
{
for (int k = 1; k < 0x38; k++)
{
this.SeedArray[k] -= this.SeedArray[1 + ((k + 30) % 0x37)];
if (this.SeedArray[k] < 0)
{
this.SeedArray[k] += 0x7fffffff;
}
}
}
this.inext = 0;
this.inextp = 0x15;
Seed = 1;
}
public virtual int Next()
{
return this.InternalSample();
}
private int InternalSample()
{
int inext = this.inext;
int inextp = this.inextp;
if (++inext >= 0x38)
{
inext = 1;
}
if (++inextp >= 0x38)
{
inextp = 1;
}
int num = this.SeedArray[inext] - this.SeedArray[inextp];
if (num < 0)
{
num += 0x7fffffff;
}
this.SeedArray[inext] = num;
this.inext = inext;
this.inextp = inextp;
return num;
}
热心网友
时间:2024-01-24 06:48
NextInt可以在你限定的证书范围内分布
热心网友
时间:2024-01-24 06:48
产生的是0到1中间的随机数
比如 0.235421 这样的
热心网友
时间:2024-01-24 06:47
NextInt可以在你限定的证书范围内分布
热心网友
时间:2024-01-24 06:48
产生的是0到1中间的随机数
比如 0.235421 这样的