问题
The normal distribution:
std::normal_distribution<T>
only accepts a real-valued type such as float
or double
, why doesn't it accept an integer type?
How can I create a normal integer distribution?
回答1:
I know next to nothing about C++11 but I know a little math (or I did at one point) and a discrete normal distribution is called a Binomial distribution. In fact a normal distribution is the binomial distribution when you let n
go to infinity.
So assuming C++11 has a binomial distribution then you have a discrete normal distribution.
Why don't you try std::binomial_distribution
?
You might also want to read up on the de Moivre–Laplace theorem.
回答2:
Just cast the generated random value to an integer.
std::normal_distribution<float> noise(0, 2.f);
randomValue = (int) round(noise(engine));
来源:https://stackoverflow.com/questions/22016007/how-can-i-create-a-normal-integer-distribution-in-c11