Random Engine Differences

2020-02-16 12:12发布

The C++11 standard specifies a number of different engines for random number generation: linear_congruential_engine, mersenne_twister_engine, subtract_with_carry_engine and so on. Obviously, this is a large change from the old usage of std::rand.

Obviously, one of the major benefits of (at least some) of these engines is the massively increased period length (it's built into the name for std::mt19937).

However, the differences between the engines is less clear. What are the strengths and weaknesses of the different engines? When should one be used over the other? Is there a sensible default that should generally be preferred?

标签: c++ random c++11
7条回答
来,给爷笑一个
2楼-- · 2020-02-16 12:37

Its a trade-off really. A PRNG like Mersenne Twister is better because it has extremely large period and other good statistical properties.

But a large period PRNG takes up more memory (for maintaining the internal state) and also takes more time for generating a random number (due to complex transitions and post processing).

Choose a PNRG depending on the needs of your application. When in doubt use Mersenne Twister, its the default in many tools.

查看更多
登录 后发表回答