为什么我的随机数生成器在 C# 中看起来不是随机的? [英] Why does it appear that my random number generator isn't random in C#?

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

问题描述

我在 Microsoft Visual C# 2008 Express 中工作.

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

我找到了这段代码:

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

        return random.Next(min, max);
    }

问题是我已经运行了 100 多次,当我的 min = 0 和 max = 1 时,它总是给我相同的答案.我每次都得到 0.(我创建了一个测试函数来运行它——真的——我每次都得到 0).我很难相信这是巧合……我还能做些什么来检查或测试这个吗?(我确实用 min = 0 和 max = 10 重新运行了测试,前 50 次,结果总是5",第二次 50 次,结果总是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...

-阿迪娜

推荐答案

min = 0 和 max = 1 的问题是 min 是包含性的,而 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天全站免登陆