0

I'm unable to tell if there is something wrong with Random number generator in this case.

Random x = new Random();
do
{
    a = x.Next(1, 200);
    aa = x.Next(1, 200);
    b = x.Next(a + 1, 200);
    bb = x.Next(aa + 1, 200);
    Console.WriteLine(a + " " + aa + "     " + b + " " + bb);

} while (a != aa || b != bb);

I'm comparing when two pairs of random numbers are the same, when they are I want to know what those numbers are. For some reason they always end up being in range of 150 - 200. Which doesn't seem random enough to me.

Basically I'm trying simulate how long it would take until 2 number lottery is won.

Is this because Random class is not random enough or is there problem with the code?

4

2 回答 2

2

类的随机性没有任何问题Random,至少不会影响您的代码。

您选择彩票号码的方式不正确。您选择的第一个数字本身得到了正确的分布,但随后您选择了另一个高于第一个的数字,这将有利于该范围内更高的数字。

此外,当第一个数字很高时,它更有可能猜测第二个数字,因为变化较小。在您的猜测中,您会看到更多的命中率更高。

除此之外,它看起来还不错。确定你猜一个数字的频率没有任何线性分布。你很少会在很早的时候就猜对了,而且你很少会在很长一段时间内都猜对了。您看到集中在特定数量的猜测周围是正常的。

于 2011-10-02T05:31:17.270 回答
1

你的代码没有做你认为它正在做的事情。首先,既不a是也b不会是 56,因为上限是独占的。第二个如果a是 54,b则永远是 55。所以有 56 分之一的机会,a分别b是 54 和 55,但应该只有 3,136 分之一的机会。

更新:此答案适用于该问题的先前版本。一旦事情稳定下来,我会用最终问题的答案来更新它。

于 2011-10-02T05:25:21.533 回答