如何在不关闭C ++程序的情况下每毫秒生成一个随机数?

时间:2018-11-21 06:00:49

标签: c++ c++11

我想每次在while循环中生成一个新的随机数,直到当循环满足while循环中的条件(如10个随机数)时中断为止。

1 个答案:

答案 0 :(得分:0)

您可以在https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

上找到一些东西
int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> dis(1, 6);

    for (int n=0; n<10; ++n)
        //Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}

for循环显然可以被while取代