The code below generate the number between [0,PI)
as default:
#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;
}
I would like generate a number between [0,PI]
. To be clear the second bracket must be ]
, not )
(with closed interval).
Could somebody tell me if the code above is correct?
That looks correct. Looking here it says the generated value will be in the range [a, b)
. Since a = 0
and b
is the smallest number greater than M_PI
, you should get a value in [0, PI]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With