@Kaper,
Here's my version of that:
Function RandPois(Lambda As Single, _
Optional bVolatile As Boolean = False) As Long
' shg 2007
' UDF or VBA
' Returns a random Poisson variate with variance Lambda
' Knuth, Seminumerical Algorithms, p 132
Dim L As Single
Dim k As Long
Dim p As Single
If bVolatile Then Application.Volatile
L = Exp(-Lambda)
p = 1
Do
k = k + 1
p = p * Rnd()
Loop Until p <= L
RandPois = k - 1
End Function
It differs from yours in that the loop always executes at least once.
I went back to Knuth, and his description is a little arm-waving. Any thought on which is correct?
Bookmarks