-1
我想知道是否有人知道如何将此随机变量的VBA代码转换为Python。 (“已保存的”变量是VBA布尔)Marsaglia bray,将VBA代码转换为Python
function MarsagliaBrayNormSRnd() As Double
Dim U1 As Double, U2 As Double
Dim x As Double, Y As Double
If Saved = False Then
x = 2
While x > 1
U1 = (2 * Rnd() - 1)
U2 = (2 * Rnd() - 1)
x = (U1 * U1) + (U2 * U2)
Wend
Y = Sqr(-2 * Log(x)/x)
MarsagliaBrayNormSRnd = U1 * Y
SavedNormSRnd = U2 * Y
Saved = True
Else
MarsagliaBrayNormSRnd = SavedNormSRnd
Saved = False
End If
End Function
我想这个代码自己,但并没有得到它的工作:
import math
import pandas as pd
import numpy as np
def MarsagliaBrayNormSRnd(u1,u1,x,y):
if Saved==False:
x = 2
while x > 1:
np.random.seed(1)
u1 = 2 * np.random.random((3,1)) - 1
u2 = 2 * np.random.random((3,1)) - 1
x = (u1 * u1) + (u2 * u2)
y = math.sqrt(-2 * math.log(x)/x)
MarsagliaBrayNormSRnd = u1 * y
SavedNormSRnd = u2 * y
Saved = True
else:
MarsagliaBrayNormSRnd = SavedNormSRnd
Saved = False
正确的压痕,什么是不工作 – depperm
VBA:'如果保存=假Then',蟒蛇'如果保存==真:'。你是真的吗? –
indentation NOT correct - 如果在相同缩进级别,请参阅def –