How do I generate a random number using the C++11 standard library

后端 未结 7 1232
遇见更好的自我
遇见更好的自我 2020-12-05 05:34

The new C++11 Standard has a whole chapter dedicated to random number generators. But how do I perform the simplest, most common task that used to be coded like this, but wi

7条回答
  •  清歌不尽
    2020-12-05 05:51

    I use the following code in my project. 'engine' and 'distribution' can be one of the provided by the library.

    #include 
    #include 
    #include 
    ...
    std::uniform_int_distribution unif;
    std::random_device rd;
    std::mt19937 engine(rd());
    std::function rnd = std::bind(unif, engine);
    
    std::cout << rnd() << '\n';
    

提交回复
热议问题