Poisson Data by Long Method - UDF Error?

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
I am trying to convert a C language code to VBA. The converted VBA UDF code is:

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
and the reference code is:
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.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top