3
我试图通过使用Box-Muller变换对Marsarlia极坐标法来测试从正态分布生成数字的速度。据说Marsaglia极坐标法被认为比Box-Muller变换更快,因为它不需要计算sin和cos。但是,当我用Python编写这个代码时,这是不正确的。有人可以验证这一点,或向我解释为什么会发生这种情况?从Python中的正态分布生成数
def marsaglia_polar():
while True:
x = (random.random() * 2) - 1
y = (random.random() * 2) - 1
s = x * x + y * y
if s < 1:
t = math.sqrt((-2) * math.log(s)/s)
return x * t, y * t
def box_muller():
u1 = random.random()
u2 = random.random()
t = math.sqrt((-2) * math.log(u1))
v = 2 * math.pi * u2
return t * math.cos(v), t * math.sin(v)
没有看到你的代码!? – tzaman
我们可以,如果你告诉我们你的代码 – inspectorG4dget
糟糕,添加!!!! – user2770287