作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面的代码默认生成[0,PI)
之间的数字:
#include <iostream>
#include <random>
int main()
{
std::random_device rd;
std::default_random_engine re(rd());
//std::uniform_real_distribution<double> unifPhi(0., M_PI);//[0.,PI) // <- default
std::uniform_real_distribution<double> unifPhi{0.0, std::nextafter(M_PI, 2.*M_PI)};//probably [0.,PI]
for(unsigned int i=0u;i<10u;++i)
std::cout << unifPhi(re) << std::endl;
return 0;
}
我想在 [0,PI]
之间生成一个数字。要清楚,第二个括号必须是 ]
,而不是 )
(闭区间)。
谁能告诉我上面的代码是否正确?
最佳答案
看起来是正确的。寻找 here它表示生成的值将在 [a, b)
范围内。由于 a = 0
和 b
是大于 M_PI
的最小数字,您应该在 [0, PI]
.
关于c++ - C++11 中的随机闭区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22581737/
我是一名优秀的程序员,十分优秀!