Generate random numbers using C++11 random library

后端 未结 6 808
梦毁少年i
梦毁少年i 2020-11-22 15:13

As the title suggests, I am trying to figure out a way of generating random numbers using the new C++11 library. I have tried it with this code:<

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 15:20

    Stephan T. Lavavej (stl) from Microsoft did a talk at Going Native about how to use the new C++11 random functions and why not to use rand(). In it, he included a slide that basically solves your question. I've copied the code from that slide below.

    You can see his full talk here: http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

    #include 
    #include 
    
    int main() {
        std::random_device rd;
        std::mt19937 mt(rd());
        std::uniform_real_distribution dist(1.0, 10.0);
    
        for (int i=0; i<16; ++i)
            std::cout << dist(mt) << "\n";
    }
    

    We use random_device once to seed the random number generator named mt. random_device() is slower than mt19937, but it does not need to be seeded because it requests random data from your operating system (which will source from various locations, like RdRand for example).


    Looking at this question / answer, it appears that uniform_real_distribution returns a number in the range [a, b), where you want [a, b]. To do that, our uniform_real_distibution should actually look like:

    std::uniform_real_distribution dist(1, std::nextafter(10, DBL_MAX));
    

提交回复
热议问题