iOS Gaussian distribution of random numbers [duplicate]

允我心安 提交于 2019-12-04 07:28:29

Just use a uniform distribution generator and apply the Box-Muller Transform:

double u1 = (double)arc4random() / UINT32_MAX; // uniform distribution
double u2 = (double)arc4random() / UINT32_MAX; // uniform distribution
double f1 = sqrt(-2 * log(u1));
double f2 = 2 * M_PI * u2;
double g1 = f1 * cos(f2); // gaussian distribution
double g2 = f1 * sin(f2); // gaussian distribution

One simple option is to add several numbers from a uniform distribution together. Many dice based games use this approach to generate roughly normal distributions of results.

via wikipedia

If you can be more specific about what distribution you want there may be more precise solutions but combining several rolls is an easy and fairly flexible solution.

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