iOS高斯分布的随机数 [英] iOS Gaussian distribution of random numbers
本文介绍了iOS高斯分布的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在Objective-C/C中生成随机的高斯双值
Possible Duplicate:
Generating a random Gaussian double in Objective-C/C
有没有办法从iOS中的高斯(正态,贝尔曲线)分布中而不是从均匀分布中获得随机数?我发现的所有随机数生成器基本上都是统一的,我想使数字围绕某个点聚类.谢谢!
Is there any way of getting a random number not from a uniform distribution, but from a Gaussian (Normal, Bell Curve) distribution in iOS? All the random number generators I have found are basically uniform and I want to make the numbers cluster around a certain point. Thanks!
推荐答案
只需使用统一的分布生成器并应用 Box-穆勒变换:
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
这篇关于iOS高斯分布的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文