正如文件所说:
如果这不是一个的影响是不确定
short
,int
,long
,long long
,unsigned short
,unsigned int
,unsigned long
,或unsigned long long
.
如果我不关心范围,我可以掩盖更大类型的位来生成随机数.如果没有,那就更复杂了.为什么默认情况下不提供字节类型?
std::binomial_distribution
我在编译clang++
(使用标准库)时遇到了一些奇怪的行为libstdc++
。
考虑以下简单的程序:
#include <ctime>
#include <random>
#include <iostream>
unsigned binom(unsigned n, double p) {
std::mt19937 e(time(NULL));
std::binomial_distribution<unsigned> b(n, p);
return b(e);
}
int main() {
std::cout << "sample1=" << binom(1073741823, 0.51174692866744709) << "\n";
std::cout << "sample2=" << binom(1073741824, 0.51174692866744709) << "\n";
}
Run Code Online (Sandbox Code Playgroud)
该程序将输出一行 ( sample1=511766586\n
),然后无限期挂起。
我是否以某种方式调用了未定义的行为?无论 PRNG 返回什么,这似乎都会发生。无论我如何播种,我main
都会坚持第二行。