开发者

Unable to determine randomness vs code issue

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 + "  开发者_StackOverflow社区   " + 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?


There is nothing wrong with the randomness of the Random class, at least nothing that would affect your code.

Your way of picking lottery numbers is incorrect. The first number that you pick gets a correct distribution in itself, but then you pick another number to be higher than the first, which will favour higher numbers in the range.

Also, when the first number is high, it's more likely to guess the second number as there is a smaller variation. In your guesses you will see considerably more hits in the higher numbers.

Other than that it looks all right. Determining how often you could guess a number doesn't have any linear distribution. You would very seldom get a right guess very early, and you would very seldom go for very long without ever getting it right. It's normal that you see a concentration around a specific number of guesses.


Your code isn't doing what you think it's doing. First, neither a nor b will ever be 56, because the upper limit is exclusive. Second if a is 54, b will always be 55. So there's a 1 in 56 chance that a and b are 54 and 55 respectively, but there should only be a 1 in 3,136 chance of that.

Update: This answer applies to a previous version of the question. Once things stabilize, I'll update it with the answer to whatever the final question works out to be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜