C# 稳健的随机数生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Robust Random Number Generation
提问by
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.
我正在寻找一种不使用特殊硬件的高性能、相当健壮的 RNG。它可以使用数学方法(Mersenne Twister 等),它可以从机器“收集熵”,无论如何。在 Linux/etc 上,我们有一个drand48()
生成 48 个随机位的。我想要一个类似的 C++ 或 C# 函数/类,它可以生成超过 32 位的随机性,并且低位与高位一样随机。
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
.
它不必加密安全,但不得使用或基于 C 语言rand()
或 .NET System.Random
。
Any source code, links to source, etc. would be appreciated! Failing that, what TYPE of RNG should I be looking for?
任何源代码、源链接等将不胜感激!否则,我应该寻找什么类型的 RNG?
采纳答案by Chris Jester-Young
For C++, Boost.Randomis 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! :-)
对于 C++,Boost.Random可能就是你要找的。它支持 MT(以及许多其他算法),并且可以通过nondet_random
该类收集熵。一探究竟!:-)
回答by Mark Harrison
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 Scientific Library(GSL) 有一组相当广泛的 RN 生成器、测试工具等。如果您使用的是 linux,它可能已经在您的系统上可用。
回答by nsanders
Watch out for the Gnu Scientific Library. It's licensed under the GPL rather than LGPL.
注意 Gnu 科学图书馆。它是根据 GPL 而不是 LGPL 获得许可的。
As other folks mentioned, the Boost random classes are a good start. Their implementation conforms to the PRNG code slated for TR1:
正如其他人提到的,Boost 随机类是一个好的开始。它们的实现符合为 TR1 指定的 PRNG 代码:
http://www.boost.org/doc/libs/1_35_0/libs/random/index.htmlhttp://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html
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
如果您有最新版本的 G++ 编译器,您可能会发现已包含 TR1 库
回答by Rohit Vipin Mathews
回答by bames53
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
.
C++11 采用了基于 boost.random 的强大的随机数库。您可以使用不同的算法访问多个随机数引擎,以满足您的质量、速度或大小要求。质量实现甚至可以访问您的平台通过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).
此外,还有许多适配器可以生成特定的发行版,从而无需手动进行此类操作(通常会错误地进行操作)。