Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I am trying to convert a C language code to VBA. The converted VBA UDF code is:
And, I am referencing the C code from:
and the reference code is:
I have tried an exact replication but my UDF is not giving adequate output and I need help for the same.
Code:
Public Function Poisson2( _
Optional ByVal C As Double = 4) As Long
If C <= 0# Then Exit Function
Dim d As Double, beta As Double, alpha As Double, k As Double, u As Double, v As Double, x As Double, n As Long, y As Double
Dim temp As Double, lhs As Double, rhs As Double
d = 0.767 - 3.36 / C
beta = Application.WorksheetFunction.pi / Sqr(3# * C)
alpha = beta * C
k = Log(Abs(d)) - C - Log(beta)
x1:
u = Rnd()
x = (alpha - Log((1# - u) / u)) / beta
n = Int(x + 0.5) - 1 * (Int(x + 0.5) > (x + 0.5)) ' Application.WorksheetFunction.Floor(x + 0.5,1)
If (n < 0) Then
v = Rnd()
y = alpha - beta * x
temp = 1# + Exp(y)
lhs = y + Log(v / (temp * temp))
rhs = k + n * Log(C) - Log(Apllication.Worksheetfunction.Fact(n))
If (lhs <= rhs) Then
Poisson2 = n
Else
GoTo x1
End If
GoTo x1
End If
End Function
And, I am referencing the C code from:
HTML:
https://www.johndcook.com/SimpleRNG.cpp
Code:
[COLOR=#000000]int SimpleRNG::PoissonLarge(double lambda)
[/COLOR]
I have tried an exact replication but my UDF is not giving adequate output and I need help for the same.