C# 的随机数生成器是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13539974/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:57:07  来源:igfitidea点击:

How does C#'s random number generator work?

c#randomnumbersgenerator

提问by Seth Taddiken

I was just wondering how the random number generator in C# works. I was also curious how I could make a program that generates random WHOLE INTEGERnumbers from 1-100.

我只是想知道 C# 中的随机数生成器是如何工作的。我也很好奇我如何制作一个程序来生成从 1 到 100 的随机整数

采纳答案by Jon Skeet

I was just wondering how the random number generator in C# works.

我只是想知道 C# 中的随机数生成器是如何工作的。

That's implementation-specific, but the wikipedia entry for pseudo-random number generatorsshould give you some ideas.

这是特定于实现的,但伪随机数生成器维基百科条目应该给你一些想法。

I was also curious how I could make a program that generates random WHOLE INTEGER numbers from 1-100.

我也很好奇我如何制作一个程序来生成从 1 到 100 的随机整数。

You can use Random.Next(int, int):

您可以使用Random.Next(int, int)

Random rng = new Random();
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(rng.Next(1, 101));
}

Note that the upper bound is exclusive- which is why I've used 101 here.

请注意,上限是唯一的- 这就是我在这里使用 101 的原因。

You should also be aware of some of the "gotchas" associated with Random- in particular, you should notcreate a new instance every time you want to generate a random number, as otherwise if you generate lots of random numbers in a short space of time, you'll see a lot of repeats. See my article on this topicfor more details.

你也应该了解一些相关联的“陷阱”的Random-特别是,你应该不是创建一个新的实例要生成一个随机数每一次,否则,如果你在很短的时间空间产生大量的随机数,你会看到很多重复。有关更多详细信息,请参阅我关于此主题的文章

回答by Zbigniew

You can use Random.Next(int maxValue):

您可以使用Random.Next(int maxValue)

Return: A 32-bit signed integer greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned.

返回:一个大于或等于零且小于 maxValue 的 32 位有符号整数;也就是说,返回值的范围通常包括零但不包括maxValue。但是,如果 maxValue 等于零,则返回 maxValue。

var r = new Random();
// print random integer >= 0 and  < 100
Console.WriteLine(r.Next(100));

For this case however you could use Random.Next(int minValue, int maxValue), like this:

但是,对于这种情况,您可以使用Random.Next(int minValue, int maxValue),如下所示:

// print random integer >= 1 and < 101
Console.WriteLine(r.Next(1, 101);)
// or perhaps (if you have this specific case)
Console.WriteLine(r.Next(100) + 1);

回答by Arrai

I've been searching the internet for RNG for a while now. Everything I saw was either TOO complex or was just not what I was looking for. After reading a few articles I was able to come up with this simple code.

我已经在互联网上搜索 RNG 一段时间了。我看到的一切要么太复杂,要么不是我想要的。在阅读了几篇文章后,我能够想出这个简单的代码。

{
  Random rnd = new Random(DateTime.Now.Millisecond);
  int[] b = new int[10] { 5, 8, 1, 7, 3, 2, 9, 0, 4, 6 };
  textBox1.Text = Convert.ToString(b[rnd.Next(10)])
}

Simple explanation,

简单的解释,

  1. create a 1 dimensional integer array.
  2. full up the array with unordered numbers.
  3. use the rnd.Next to get the position of the number that will be picked.
  1. 创建一个一维整数数组。
  2. 用无序数字填满数组。
  3. 使用 rnd.Next 获取将被选取的数字的位置。

This works well.

这很好用。

To obtain a random number less than 100 use

要获得小于 100 的随机数,请使用

{
  Random rnd = new Random(DateTime.Now.Millisecond);
  int[] b = new int[10] { 5, 8, 1, 7, 3, 2, 9, 0, 4, 6 };
  int[] d = new int[10] { 9, 4, 7, 2, 8, 0, 5, 1, 3, 4 };
  textBox1.Text = Convert.ToString(b[rnd.Next(10)]) + Convert.ToString(d[rnd.Next(10)]);
}

and so on for 3, 4, 5, and 6 ... digit random numbers.

依此类推 3、4、5 和 6 ... 位随机数。

Hope this assists someone positively.

希望这对某人有积极的帮助。