C++ Predictable Rand() Output

前端 未结 6 433
陌清茗
陌清茗 2021-01-14 14:57

After noticing that the rand() function produced the same output of 41 each time, I seeded the generator using srand(time(0)). That solved the problem of the recurring outpu

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 15:30

    I have no idea why you are getting those results, but there are better ways in C++11:

    #include 
    #include 
    int main()
    {
        auto rnd = std::default_random_engine(std::random_device{}());
        std::uniform_int_distribution<> dis(1, 999);
        std::cout << dis(rnd) << '\n';
    }
    

提交回复
热议问题