我只是想知道 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/