'' This is an implementation of the algorithm used in Numerical
' Recipe's ran0 generator. It is the same as MINSTD with an XOR mask
' of 123459876 on the seed.
' The period of this generator is 2^31.
' Note, if you choose a seed of 123459876 it would give a degenerate
' series 0,0,0,0, ... I've made that into an error.
Public Class RandomUniform
Private Const m As Long = 2147483647
Private Const a As Long = 16807
Private Const q As Long = 127773
Private Const r As Long = 2836
Private Const mask As Long = 123459876
Private state As Long
Private Function randomGet() As Long
Dim h As Long = state / q
Dim t As Long = a * (state - h * q) - h * r
If t < 0 Then
state = t + m
Else
state = t
End If
Return state
End Function
Public Function randomUniform() As Double
Return randomGet() / 2147483647.0
End Function
Public Sub randomSetSeed(ByVal s As Long)
If s = mask Then
Console.WriteLine("Error")
End If
state = s Xor mask
End Sub
Public Function randomUniformPositive() As Double
Dim x As Double
Do
x = randomUniform()
Loop While x = 0
Return x
End Function
End Class