你如何在C#中生成一个随机数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you generate a random number in C#?
提问by lillq
I would like to generate a random floating point number between 2 values. What is the best way to do this in C#?
我想在 2 个值之间生成一个随机浮点数。在 C# 中执行此操作的最佳方法是什么?
采纳答案by OwenP
The only thing I'd add to Eric's response is an explanation; I feel that knowledge of why code works is better than knowing what code works.
我唯一要添加到Eric的回复中的是解释;我觉得了解代码为什么起作用比知道什么代码起作用要好。
The explanation is this: let's say you want a number between 2.5 and 4.5. The range is 2.0 (4.5 - 2.5). NextDouble
only returns a number between 0 and 1.0, but if you multiply this by the range you will get a number between 0 and range.
解释是这样的:假设您想要一个介于 2.5 和 4.5 之间的数字。范围是 2.0 (4.5 - 2.5)。 NextDouble
只返回一个介于 0 和 1.0 之间的数字,但如果将其乘以范围,您将得到一个介于 0 和范围之间的数字。
So, this would give us random doubles between 0.0 and 2.0:
所以,这会给我们 0.0 和 2.0 之间的随机双打:
rng.NextDouble() * 2.0
But, we want them between 2.5 and 4.5! How do we do this? Add the smallest number, 2.5:
但是,我们希望它们介于 2.5 和 4.5 之间!我们如何做到这一点?添加最小的数字 2.5:
2.5 + rng.NextDouble() * 2.0
Now, we get a number between 0.0 and 2.0; if you add 2.5 to each of these values we see that the range is now between 2.5 and 4.5.
现在,我们得到一个介于 0.0 和 2.0 之间的数字;如果将 2.5 添加到这些值中的每一个,我们会看到范围现在介于 2.5 和 4.5 之间。
At first I thought that it mattered if b > a or a > b, but if you work it out both ways you'll find it works out identically so long as you don't mess up the order of the variables used. I like to express it with longer variable names so I don't get mixed up:
起初我认为 b > a 或 a > b 很重要,但如果你用两种方式解决它,你会发现只要你不弄乱所用变量的顺序,它的效果是一样的。我喜欢用更长的变量名来表达它,这样我就不会混淆:
double NextDouble(Random rng, double min, double max) { return min + (rng.NextDouble() * (max - min)); }
回答by Ryan Farley
// generate a random number starting with 5 and less than 15
Random r = new Random();
int num = r.Next(5, 15);
For doubles you can replace Next with NextDouble
对于双打,您可以用 NextDouble 替换 Next
回答by Eric
System.Random r = new System.Random();
double rnd( double a, double b )
{
return a + r.NextDouble()*(b-a);
}
回答by enigmatic
How random? If you can deal with pseudo-random then simply:
有多随机?如果您可以处理伪随机,那么只需:
Random randNum = new Random();
randNum. NextDouble(Min, Max);
If you want a "better" random number, then you probably should look at the Mersenne Twister algorithm. Plenty of people hav already implemented itfor you though
回答by Sameer Alibhai
Here is a snippet of how to get Cryographically safe random numbers: This will fill in the 8 bytes with a crytographically strong sequence of random values.
这是如何获得加密安全随机数的片段:这将用加密强的随机值序列填充 8 个字节。
byte[] salt = new byte[8];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(salt);
For more details see How Random is your Random??"(inspired by a CodingHorror article on deck shuffling)
有关更多详细信息,请参阅您的随机性有多随机?”(灵感来自关于甲板洗牌的 CodingHorror 文章)
回答by Andrew Burns
For an explaination of why Longhorn has been downmodded so much: http://msdn.microsoft.com/en-us/magazine/cc163367.aspxLook for the implementation of NextDouble and the explanation of what is a random double.
关于为什么 Longhorn 被降级了这么多的解释:http: //msdn.microsoft.com/en-us/magazine/cc163367.aspx寻找 NextDouble 的实现和什么是随机双倍的解释。
That link is also a goo example of how to use cryptographic random numbers (like Sameer mentioned) only with actual useful outputs instead of a bit stream.
该链接也是如何仅将加密随机数(如 Sameer 提到的)用于实际有用的输出而不是位流的示例。