Random Engine Differences

前端 未结 7 1391
灰色年华
灰色年华 2020-12-13 02:06

The C++11 standard specifies a number of different engines for random number generation: linear_congruential_engine, mersenne_twister_engine,

7条回答
  •  無奈伤痛
    2020-12-13 02:30

    In general, mersenne twister is the best (and fastest) RNG, but it requires some space (about 2.5 kilobytes). Which one suits your need depends on how many times you need to instantiate the generator object. (If you need to instantiate it only once, or a few times, then MT is the one to use. If you need to instantiate it millions of times, then perhaps something smaller.)

    Some people report that MT is slower than some of the others. According to my experiments, this depends a lot on your compiler optimization settings. Most importantly the -march=native setting may make a huge difference, depending on your host architecture.

    I ran a small program to test the speed of different generators, and their sizes, and got this:

    std::mt19937 (2504 bytes): 1.4714 s
    std::mt19937_64 (2504 bytes): 1.50923 s
    std::ranlux24 (120 bytes): 16.4865 s
    std::ranlux48 (120 bytes): 57.7741 s
    std::minstd_rand (4 bytes): 1.04819 s
    std::minstd_rand0 (4 bytes): 1.33398 s
    std::knuth_b (1032 bytes): 1.42746 s
    

提交回复
热议问题