<分区>
键入两个语句,使用 rand()
打印 2 个介于(包括)100 和 149 之间的随机整数。以换行符结尾。例如:
101
133
我试过...
cout << rand() % 100 << endl;
cout << rand() % 149 << endl;
...无法让它工作。
我很困惑。任何帮助,将不胜感激!
<分区>
键入两个语句,使用 rand()
打印 2 个介于(包括)100 和 149 之间的随机整数。以换行符结尾。例如:
101
133
我试过...
cout << rand() % 100 << endl;
cout << rand() % 149 << endl;
...无法让它工作。
我很困惑。任何帮助,将不胜感激!
最佳答案
Don't use (rand() % 50) + 100
如果您想要均匀分布。
使用 std::uniform_int_distribution
来自 <random>
标题:
#include <random>
#include <iostream>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(100, 149);
for (int n=0; n<2; ++n)
std::cout << dis(gen) << ' ';
std::cout << '\n';
}
关于c++ - 100 和 149 之间(包括)的随机数的固定范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25572329/