Random
2021-11-08 本文已影响0人
杰罗xr
获取随机数1-6
包含1也包含6
https://www.learncpp.com/cpp-tutorial/random-number-generation/
#include <iostream>
#include <random> // for std::mt19937
#include <ctime> // for std::time
int main()
{
// Initialize our mersenne twister with a random seed based on the clock
std::mt19937 mersenne{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
// Create a reusable random number generator that generates uniform numbers between 1 and 6
std::uniform_int_distribution die{ 1, 6 };
// If your compiler doesn't support C++17, use this instead
// std::uniform_int_distribution<> die{ 1, 6 };
// Print a bunch of random numbers
for (int count{ 1 }; count <= 48; ++count)
{
std::cout << die(mersenne) << '\t'; // generate a roll of the die here
// If we've printed 6 numbers, start a new row
if (count % 6 == 0)
std::cout << '\n';
}
return 0;
}
输出结果
3 4 1 2 2 1
3 4 1 6 3 4
2 5 5 2 3 3
3 2 1 5 2 4
2 2 1 6 3 3
5 6 2 4 3 5
5 6 6 6 1 4
3 4 5 1 5 5