gpt4 book ai didi

c# - 使用 C# 的随机数

转载 作者:可可西里 更新时间:2023-11-01 08:31:02 25 4
gpt4 key购买 nike

我想生成一个介于 1 到 500 万之间的随机数。这个过程不一定要很快(虽然如果很快的话会很好),但它必须尽可能随机(我知道没有什么是随机的)。我有各种种子数据源。

我不确定 .NET Random class 就足够了。

这将用于选择中奖彩票。

最佳答案

System.Random类可能已经足够好了:

Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

您唯一需要注意的是不要太频繁地重复使用同一个种子:

If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random.

关于c# - 使用 C# 的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2261831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com