它为什么出现我的随机数发生器不是在C#随机? [英] Why does it appear that my random number generator isn't random in C#?

查看:96
本文介绍了它为什么出现我的随机数发生器不是在C#随机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Microsoft Visual C#2008年工作前preSS。

I'm working in Microsoft Visual C# 2008 Express.

我发现这个片段code的:

I found this snippet of code:

    public static int RandomNumber(int min, int max)
    {
        Random random = new Random();

        return random.Next(min, max);
    }

问题是,我已经运行超过100倍,它总是给我相同的答案时,我的分= 0,最大值= 1,我得到的每一次0。 (我创建了一个测试功能来运行它 - 真的 - 我每次都得到0)。我有一个很难相信这是一个巧合......是有什么我可以做检验或测试呢? (我也重新运行与分= 0,最大值= 10和第一50ish次测试,结果总是5,第二次50ish,结果总是9。

the problem is that I've run it more than 100 times, and it's ALWAYS giving me the same answer when my min = 0 and max = 1. I get 0 every single time. (I created a test function to run it - really - I'm getting 0 each time). I'm having a hard time believing that's a coincidence... is there something else I can do to examine or test this? (I did re-run the test with min = 0 and max = 10 and the first 50ish times, the result was always "5", the 2nd 50ish times, the result was always "9".

?我需要的东西多了几分随意一致...

?? I need something a little more consistently random...

-Adeena

推荐答案

与分= 0,最大值= 1的问题是分是包容和max是排他性的。因此,对于组合中的唯一可能的值是0。

The problem with min = 0 and max = 1 is that min is inclusive and max is exclusive. So the only possible value for that combination is 0.

这篇关于它为什么出现我的随机数发生器不是在C#随机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆