Is there an alternative to using time to seed a random number generation?

前端 未结 10 635
既然无缘
既然无缘 2020-11-27 06:30

I\'m trying to run several instances of a piece of code (2000 instances or so) concurrently in a computing cluster. The way it works is that I submit the jobs and the clust

10条回答
  •  Happy的楠姐
    2020-11-27 07:05

    If C++11 can be used then consider std::random_device. I would suggest you to watch link for a comprehensive guide.

    Extracting the essential message from the video link : You should never use srand & rand, but instead use std::random_device and std::mt19937 -- for most cases, the following would be what you want:

    #include 
    #include 
    int main() {
        std::random_device rd;
        std::mt19937 mt(rd());
        std::uniform_int_distribution dist(0,99);
        for (int i = 0; i < 16; i++) {
            std::cout << dist(mt) << " ";
        }
        std::cout << std::endl;
    }
    

提交回复
热议问题