gpt4 book ai didi

objective-c - 在 Objective-C/C 中生成随机高斯 double

转载 作者:太空狗 更新时间:2023-10-30 03:36:56 25 4
gpt4 key购买 nike

我正在尝试在 Objective-C 中生成一个随机高斯 double (与 Java 中的 random.nextGaussian 相同)。但是 rand_gauss() 似乎不起作用。有人知道实现此目标的方法吗?

最佳答案

This link展示了如何使用标准的 random() 函数计算它。

我应该指出,您可能必须制作 ranf() 例程,将 random() 的输出从 [0,MAX_INT] 转换为 来自 [0,1],但这应该不会太难。

来自链接的文章:

The polar form of the Box-Muller transformation is both faster and more robust numerically. The algorithmic description of it is: float x1, x2, w, y1, y2;

     do {
x1 = 2.0 * ranf() - 1.0;
x2 = 2.0 * ranf() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );

w = sqrt( (-2.0 * ln( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;

关于objective-c - 在 Objective-C/C 中生成随机高斯 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8779843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com