gpt4 book ai didi

c++ - C++11 中的随机闭区间

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:40:46 24 4
gpt4 key购买 nike

下面的代码默认生成[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 = 0b 是大于 M_PI 的最小数字,您应该在 [0, PI].

关于c++ - C++11 中的随机闭区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22581737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com