C++ 中的确定性随机数流 STL
我想提供一个数字,然后收到一组随机数。 但是,我希望这些数字是相同的,无论我在哪台计算机上运行它(假设我提供相同的种子)。
基本上我的问题是:在 C++ 中,如果我使用 rand()
,但为 srand()
提供用户定义的种子而不是当前时间,我会吗能够在任何计算机上生成相同的随机数流?
I want to supply a number, and then receive a set of random numbers. However, I want those numbers to be the same regardless of which computer I run it on (assuming I supply the same seed).
Basically my question is: in C++, if I make use of rand()
, but supply srand()
with a user-defined seed rather than the current time, will I be able to generate the same random number stream on any computer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有数十个 PRNG 可用作库。 选一个。 我倾向于使用 Mersenne Twister。
通过使用外部提供的库,您可以避免语言库
rand()
实现奇怪或有错误的风险。 只要您的平台都符合相同的数学语义,您就会得到一致的结果。MT 是我的最爱,因为我是一名物理学家,我将这些东西用于蒙特卡洛,其中保证高维度的均匀分布非常重要。 但是不要将 MT 用作加密 PRNG!
There are dozens of PRNGs available as libraries. Pick one. I tend to use Mersenne Twister.
By using an externally supplied library, you bypass the risk of a weird or buggy implementation of your language's library
rand()
. As long as your platforms all conform to the same mathematical semantics, you'll get consistent results.MT is a favorite of mine because I'm a physicist, and I use these things for Monte Carlo, where the guarantee of equal-distribution to high dimensions is important. But don't use MT as a cryptographic PRNG!
srand()
&rand()
不是 STL 的一部分。 它们实际上是 C 运行时的一部分。是的,只要是
srand()/rand()
的相同实现,它们就会产生相同的结果。根据您的需求,您可能需要考虑使用 Boost.Random 。 它提供了几种高质量的随机数生成器。
srand()
&rand()
are not part of the STL. They're actually part of the C runtime.Yes, they will produce the same results as long as it's the same implementation of
srand()/rand()
.Depending on your needs, you might want to consider using Boost.Random. It provides several high-quality random number generators.
假设 rand() 的实现是相同的,是的。
确保这一点的最简单方法是在您的程序中包含已知的 rand() 实现 - 要么包含在项目的源代码中,要么以您可以管理的库的形式包含。
Assuming the implementations of
rand()
are the same, yes.The easiest way to ensure this is to include a known rand() implementation with your program - either included in your project's source code or in the form of a library you can manage.
编写您自己的伪随机数例程。 互联网上有很多算法记录,并且它们有许多应用程序,但 rand 不够好(例如 柏林噪音)。
对于初学者,请尝试以下链接:
http://en.wikipedia.org/wiki/Linear_congruential_generator
< a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Pseudorandom_number_generator
Write your own pseudorandom number routine. There are a lot of algorithms documented on the internet, and they have a number of applications where rand isn't good enough (e.g. Perlin Noise).
Try these links for starters:
http://en.wikipedia.org/wiki/Linear_congruential_generator
http://en.wikipedia.org/wiki/Pseudorandom_number_generator
不,ANSI C 标准仅指定
rand()
必须生成 0 到 RAND_MAX 之间的随机整数流,该整数必须至少为 32767 (来源)。 该流必须是确定性的,只是对于给定机器上的给定实现,它必须在给定相同种子的情况下生成相同的整数流。您想要一个便携式 PRNG。 Mersenne Twister(许多实现链接在底部)非常可移植,Ben Pfaff 自行开发的符合 C99 的 PRNG。 Boost.Random 也应该没问题; 当您用 C++ 编写代码时,使用 Boost 不会对您的平台选择产生太大限制(尽管一些“较小”(即不兼容)编译器可能会因大量使用模板元编程而遇到问题)。 对于小批量嵌入式平台和可能新颖的研究架构来说,这只是一个真正的问题,因此,如果“任何计算机”是指“GCC 目标的任何 x86/PPC/ARM/SPARC/Alpha/等平台”,则任何上面应该就可以了。
No, the ANSI C standard only specifies that
rand()
must produce a stream of random integers between 0 and RAND_MAX, which must be at least 32767 (source). This stream must be deterministic only in that, for a given implementation on a given machine, it must produce the same integer stream given the same seed.You want a portable PRNG. Mersenne Twister (many implementations linked at the bottom) is pretty portable, as is Ben Pfaff's homegrown C99-compliant PRNG. Boost.Random should be fine too; as you're writing your code in C++, using Boost doesn't limit your choice of platforms much (although some "lesser" (i.e. non-compliant) compilers may have trouble with its heavy use of template metaprogramming). This is only really a problem for low-volume embedded platforms and perhaps novel research architectures, so if by "any computer" you mean "any x86/PPC/ARM/SPARC/Alpha/etc. platform that GCC targets", any of the above should do just fine.
我相信,如果您提供具有相同种子的 srand,您会得到相同的结果。 这几乎就是伪随机数生成器中种子的定义。
I believe if you supply with srand with the same seed, you will get the same results. That's pretty much the definition of a seed in terms of pseudo random number generators.
是的。 对于给定的种子(起始值),rand() 返回的数字序列将始终相同。
Yes. For a given seed (starting value), the sequence of numbers that rand() returns will always be the same.