Poisson Data by Long Method - UDF Error?

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
347
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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