我想每次在while
循环中生成一个新的随机数,直到当循环满足while
循环中的条件(如10个随机数)时中断为止。
答案 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
取代