简体   繁体   English

在C ++ 11中使用哪个伪随机数生成器?

[英]Which Pseudo-Random Number Generator to use in C++11?

C++11 comes with a set of PRNG's. C ++ 11附带了一组PRNG。

In what situation should one choose one over another? 在什么情况下应该选择一个而不是另一个? What are their advantages, disadvantages etc. 它们有什么优点,缺点等等。

I think the Mersenne twister std::mt19937 engine is just fine as the "default" PRNG. 我认为Mersenne twister std::mt19937引擎就像“默认”PRNG一样好。

You can just use std::random_device to get a non-deterministic seed for mt19937 . 你可以只使用std::random_device获得非确定性种子mt19937

There is a very interesting talk from GoingNative 2013 by Stephan T. Lavavej : 来自Stephan T. Lavavej的 GoingNative 2013有一个非常有趣的演讲:

rand() Considered Harmful rand()认为是有害的

You can download the slides as well from that web site. 您也可以从该网站下载幻灯片。 In particular, slide #23 clearly compares mt19937 vs. random_device : 特别是,幻灯片#23清楚地比较了mt19937random_device

  • mt19937 is: mt19937是:
    • Fast (499 MB/s = 6.5 cycles/byte for me) 快速 (499 MB / s = 6.5个周期/字节对我来说)
    • Extremely high quality, but not cryptographically secure 极高的质量,但不是加密安全
    • Seedable (with more than 32 bits if you want) 可种子(如果你想要超过32位)
    • Reproducible (Standard-mandated algorithm) 可重复(标准强制算法)
  • random_device is: random_device是:
    • Possibly slow (1.93 MB/s = 1683 cycles/byte for me) 可能很慢(1.93 MB / s = 1683个周期/字节对我来说)
    • Strongly platform-dependent (GCC 4.8 can use IVB RDRAND) 强烈依赖平台(GCC 4.8可以使用IVB RDRAND)
    • Possibly crypto-secure (check documentation, true for VC) 可能是加密安全的(检查文档,对VC来说是真的)
    • Non-seedable, non-reproducible 不可种子,不可重复

The trade-off is speed, memory foot-print and period of PRNG. 权衡是速度,记忆足迹和PRNG的时期。

  1. Linear Congruential Generators: fast, low memory, small period 线性同余生成器:快速,低内存,小周期

  2. Lagged Fibonacci(Subtract with Carry): fast, large memory, large period 滞后斐波纳契(随运载减):快速,大内存,大周期

  3. Mersenne Twister: slow, very large memory, very large period Mersenne Twister:缓慢,非常大的记忆,非常大的时期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM