【发布时间】:2019-07-21 17:40:24
【问题描述】:
我真的不喜欢 rand() 函数。我想使用该库,但我真的不知道如何设置一个范围,例如从 1 到 3。我想“随机”这些数字( 1,2,3) 而不是像 243245 这样的大数字。这段代码是您如何使用随机库并打印随机数的方法
#include <iostream>
#include <string>
#include <random>
using namespace std;
int main()
{
minstd_rand simple_rand;
simple_rand.seed(NULL);
for (int ii = 0; ii < 10; ++ii)
{
std::cout << simple_rand() << '\n';
}
}
【问题讨论】:
-
fyi STL 先生关于使用
channel9.msdn.com/Events/GoingNative/2013/… 的有趣演讲