gpt4 book ai didi

c# - 关于默认的随机种子,它会在不同的系统中改变吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:04 24 4
gpt4 key购买 nike

我知道默认的 c# Random() 是一个伪随机数序列。我只需要这样的一个,我不是在寻找真正随机的问题。

请允许我提问:

我试过:new Random(100000000).Next(999999999),我得到了 145156561,就像一些 other people said .

我的问题是:在不同的系统(win2003、win2008、mono 等)、不同的 .net 版本(.net 3、.net 4)下,相同种子的伪随机数序列 会发生变化吗、c# 2、c# 3 等),还是会在任何其他环境中发生变化?

我想知道的是,一旦我编码,我是否总是在任何地方、现在和将来都能得到相同的伪随机数序列

最佳答案

不可靠。一般来说,弄清楚这种事情的方法是查看文档(MSDN for .NET)。如果算法未被描述并且不是以其他方式发布的算法的实现,则应将其视为实现细节并可能发生变化。

您也需要非常严格地阅读这些文档 - 如果有解释的余地​​,您需要假设最坏的情况。在这种情况下:http://msdn.microsoft.com/en-us/library/system.random.aspx仅声明:

Providing an identical seed value to different Random objects causeseach instance to produce identical sequences of random numbers.

它适用于框架:

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client Profile

Supported in: 4, 3.5 SP1 Portable Class Library

Supported in: Portable Class Library

这并没有说明它是否保证在任何 future 版本中都以相同的方式工作,甚至您是否会从各种受支持的框架中获得相同的结果。事实上,它指出:

Notes to Callers

The implementation of the random number generator inthe Random class is not guaranteed to remain the same across majorversions of the .NET Framework. As a result, your application codeshould not assume that the same seed will result in the samepseudo-random sequence in different versions of the .NET Framework.

关于c# - 关于默认的随机种子,它会在不同的系统中改变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079653/

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