鲁棒的随机数生成
我正在寻找一个高性能、相当强大的 RNG,无需使用特殊硬件。 它可以使用数学方法(Mersenne Twister 等),它可以从机器“收集熵”,等等。 在 Linux/etc 上,我们有一个 drand48()
可以生成 48 个随机位。 我想要一个类似的 C++ 或 C# 函数/类,它可以生成超过 32 位的随机性,并且低阶位与高阶位一样随机。
它不必是加密安全的,但不得使用或基于 C 语言 rand()
或 .NET System.Random
。
任何源代码、来源链接等将不胜感激! 如果做不到这一点,我应该寻找什么类型的 RNG?
I'm looking for a performant, reasonably robust RNG using no special hardware. It can use mathematical methods (Mersenne Twister, etc), it can "collect entropy" from the machine, whatever. On Linux/etc we have a drand48()
which generates 48 random bits. I'd like a similar function/class for C++ or C# which can generate more than 32 bits of randomness and which low-order bits are equally as random as high-order bits.
It doesn't have to be cryptographically secure but it must not use or be based on the C-language rand()
or .NET System.Random
.
Any source code, links to sources, etc. would be appreciated! Failing that, what TYPE of RNG should I be looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C++11采用了基于boost.random的健壮的随机数库。 您可以使用不同的算法访问多个随机数引擎,以满足您的质量、速度或大小要求。 高质量的实现甚至可以通过
std::random_device
访问您的平台提供的任何非确定性 RNG。此外,还有许多适配器可以生成特定的发行版,从而无需手动进行此类操作(这通常是错误的)。
#include
C++11 has adopted a robust random number library based on boost.random. You can access a number of random number engines using different algorithms to meet your quality, speed, or size requirements. Quality implementations will even provide access to whatever non-deterministic RNG your platform offers via
std::random_device
.In addition there are many adaptors to produce specific distributions, eliminating the need to do such manipulation by hand (something often done incorrectly).
#include <random>
Boost.Random
是我对 RNG 的首选http://www.boost .org/doc/libs/random
Boost.Random
is my first choice for RNGhttp://www.boost.org/doc/libs/random
Gnu 科学库 (GSL) 拥有一套相当广泛的 RN 生成器、测试工具等。如果您使用的是 Linux,它可能已经在您的系统上可用。
The Gnu Scientific Library (GSL) has a pretty extensive set of RN generators, test harness, etc. If you're on linux, it's probably already available on your system.
留意 Gnu 科学图书馆。 它是根据 GPL 而不是 LGPL 获得许可的。
正如其他人提到的,Boost 随机类是一个好的开始。 它们的实现符合 TR1 的 PRNG 代码:
http:// /www.boost.org/doc/libs/1_35_0/libs/random/index.html
http://www.open-std。 org/jtc1/sc22/wg21/docs/papers/2003/n1452.html
如果您有最新版本的 G++ 编译器,您可能会发现 TR1 库已包含在内
Watch out for the Gnu Scientific Library. It's licensed under the GPL rather than LGPL.
As other folks mentioned, the Boost random classes are a good start. Their implementation conforms to the PRNG code slated for TR1:
http://www.boost.org/doc/libs/1_35_0/libs/random/index.html
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html
If you have a recent version of the G++ compiler, you may find the TR1 libraries already included
对于 C++,Boost.Random 可能就是您正在寻找的。 它支持 MT(以及许多其他算法),并且可以通过
nondet_random
类收集熵。 一探究竟! :-)For C++, Boost.Random is probably what you're looking for. It has support for MT (among many other algorithms), and can collect entropy via the
nondet_random
class. Check it out! :-)