简体   繁体   English

如何在C#中生成随机数?

[英]How do you generate a random number in C#?

I would like to generate a random floating point number between 2 values. 我想在2个值之间生成一个随机浮点数。 What is the best way to do this in C#? 用C#做到这一点的最佳方法是什么?

The only thing I'd add to Eric 's response is an explanation; 我要添加到Eric响应中的唯一一件事是解释。 I feel that knowledge of why code works is better than knowing what code works. 我认为,了解为什么代码有效比知道什么代码有效更好。

The explanation is this: let's say you want a number between 2.5 and 4.5. 解释是这样的:假设您想要一个介于2.5和4.5之间的数字。 The range is 2.0 (4.5 - 2.5). 范围是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 . NextDouble仅返回0到1.0之间的数字,但是如果将其乘以范围,则会得到0到range之间的数字。

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! 但是,我们希望它们在2.5到4.5之间! How do we do this? 我们如何做到这一点? Add the smallest number, 2.5: 加上最小的数字2.5:

2.5 + rng.NextDouble() * 2.0

Now, we get a number between 0.0 and 2.0; 现在,我们得到一个介于0.0和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. 如果您在每个这些值上加上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. 起初,我认为b> a或a> b是很重要的,但是如果您用两种方式进行计算,只要您不弄乱所使用的变量的顺序,都可以找到相同的结果。 I like to express it with longer variable names so I don't get mixed up: 我喜欢用更长的变量名来表达它,所以我不会混淆:

double NextDouble(Random rng, double min, double max)
{
    return min + (rng.NextDouble() * (max - min));
}
System.Random r = new System.Random();

double rnd( double a, double b )
{
   return a + r.NextDouble()*(b-a);
}
// 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 对于双打,您可以将Next替换为NextDouble

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. 如果您想要一个“更好”的随机数,那么您可能应该看一下Mersenne Twister算法。 Plenty of people hav already implemented it for you though 虽然有很多人已经为您实施了它

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文章)

For an explaination of why Longhorn has been downmodded so much: http://msdn.microsoft.com/en-us/magazine/cc163367.aspx Look for the implementation of NextDouble and the explanation of what is a random double. 有关为何Longhorn为何如此低调的解释: http : //msdn.microsoft.com/zh-cn/magazine/cc163367.aspx寻找NextDouble的实现以及什么是随机double的解释。

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提到的)与实际有用的输出一起使用,而不是比特流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM