C++11丢弃PRNG序列中的数字
C++11 discarding numbers in a PRNG sequence
我正试图使用函数discard
跳过随机数序列中的数字。这是我的尝试:
#include <random>
#include <iostream>
using namespace std;
int main ()
{
unsigned seed = 1;
uniform_real_distribution<> dis(0,10);
mt19937 gen (seed);
cout << dis(gen) << endl;
//gen.discard(1); // supposed to be the same of `dis(gen)`?
cout << dis(gen) << endl;
cout << dis(gen) << endl;
}
该代码的输出是
9.97185
9.32557
1.28124
如果我用gen.discard(1)
取消注释行,我会得到
9.97185
0.00114381
3.02333
但我原以为前两个数字是9.97185
和1.28124
,因为数字9.32557
会被跳过。
Q:如何正确使用discard
,或者,是否有与我想要的效果相同的替代解决方案?我可以简单地使用dis(gen)
,但还有其他方法吗?
在生成下一个结果时,分发可以多次调用生成器,因此要获得所需的输出,需要将生成器的状态提前匹配的调用次数。作为一个快速测试,如果我们安装自己的生成器来计算的调用次数
struct my_mt19937 : mt19937
{
using mt19937::mt19937;
unsigned called = 0;
mt19937::result_type operator()()
{
++called;
return mt19937::operator()();
}
unsigned invocations()
{
auto result = called;
called = 0;
return result;
}
};
然后使用
unsigned seed = 1;
uniform_real_distribution<> dis(0,10);
my_mt19937 gen (seed);
cout << dis(gen) << endl;
cout << gen.invocations() << endl;
cout << dis(gen) << endl;
cout << gen.invocations() << endl;
cout << dis(gen) << endl;
cout << gen.invocations() << endl;
这在gcc 上产生以下结果
9.97185
2
9.32557
2
1.28124
2
因此,生成器被调用两次以产生每个结果。现在,如果我们修改您的示例以调用gen.discard(2);
,它将产生您期望的结果。
9.97185
1.28124
9.99041
我不知道有什么可移植的方法来确定分发将调用生成器的次数,所以丢弃中间结果的最佳方法可能是调用dis(gen);
并忽略该结果。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- OpenGL在启用深度测试时不会丢弃我的碎片
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 为什么linear_congruenty_engine :: seed(sseq)丢弃了由种子序列生成的三个数字
- C++11丢弃PRNG序列中的数字
- 丢弃部分行并保存任意长的数字