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

标签 c# random numbers generator

我只是想知道 C# 中的随机数生成器是如何工作的。我也很好奇如何制作一个程序来生成 1-100 之间的随机WHOLE INTEGER 数字。

最佳答案

您可以使用 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.

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

对于这种情况,您可以使用 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);

关于c# - C# 的随机数生成器是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13539974/

相关文章:

c# - 哪种数据包捕获格式更好?

java - 为什么 Collections#shuffle 不使用 ThreadLocalRandom?

c# - 如何检测文件何时放入我的文件夹之一

python - 线性同余生成器——如何选择种子和统计检验

python - 如何在Python中生成2个数字之间给定小数点的随机数?

java - 找出和为 15 的最小数字对

c# - 格式字符串/数字 "NNNNN"

HTML5 input type=number 和 placeholder 属性

c# - 如何以编程方式展平 SVG 文件中的变换?

c# - WP7 - 文本框光标位置错误