我正在使用seed
和srand()
或random()
这样的函数来生成0 to 5
的数字,但我需要一个更容易找到的方法{{1通过喂食数组。请建议一个
答案 0 :(得分:0)
这是一个简单的例子,可生成范围为-42到42的10个均匀分布的随机数:
#include <iostream>
#include <functional>
#include <random>
int main()
{
std::random_device seed_device;
std::mt19937 engine(seed_device());
std::uniform_int_distribution<int> distribution(-42, 42);
auto rnd = std::bind(distribution, std::ref(engine));
for (int i = 0; i < 10; ++i) {
std::cout << "random number: " << rnd() << "\n";
}
return 0;
}
另见: