How can I create a normal integer distribution in C++11?

你。 提交于 2021-02-10 13:07:40

问题


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.

enter image description here




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!