提升随机数不变
Boost Random Number Not Change
本文关键字:随机数 更新时间:2023-10-16
我在 CPP 中使用提升库生成随机数时遇到了一些问题。当我尝试打印随机数时,该值返回相同的值。这是我的代码。
for(int i = 0; i < TOTAL_PARTICLES; i++) {
boost::random::mt19937 engine(static_cast<unsigned int>(std::time(0)));
randn = boost::bind(boost::random::uniform_real_distribution<>(-2.5, 2.5), engine);
cout << "Random : " << randn() << endl;
}
您每次都使用相同的引擎种子,因此每次都会收到相同的随机数集。因此,只需在 for 循环之外播种引擎,如下所示:
boost::random::mt19937 engine(static_cast<unsigned int>(std::time(0)));
for(int i = 0; i < TOTAL_PARTICLES; i++)
{
randn = boost::bind(boost::random::uniform_real_distribution<>(-2.5, 2.5), engine);
cout << "Random : " << randn() << endl;
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 整数不会重复超过随机数
- if数组上的随机数
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 半随机数生成C++
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 有没有办法在 c++ 中同时生成随机数?如果没有,是否有解决方法?
- 具有随机数的二维数组不会更改
- 随机数未达到限制
- 我想生成许多矩阵并用随机数填充它
- <random>在实践中应该实际使用哪个随机数引擎? std::mt19937?
- 制作具有平均值的随机数生成器
- 为什么 rand 不在我的代码中生成随机数?
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 从给定种子生成相同的随机数序列C++
- 随机数生成函数说明
- C++ 尝试生成随机数时弹出警告帮助?
- 尝试在 c++ 中创建随机数生成器并收到错误
- 从给定的随机数组值生成随机数 Arduino程序C++
- 如何在个人计算机和群集 (c++) 上生成相同的随机数