Gaussian Pseudo-Random Number Generation
Algorithm creator(s)
n/a
PB author(s)
Peter P Stephensen
Description
Returns a gaussian pseudo-random number.
Note
Based on a polar form of the Box-Muller transformation
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/24013-gaussian-normal-random-numbers
See also
n/a
Source Code
Download source code file gaussian.bas (Right-click -> "Save as ...")
'NRND.INC
'
#if not %def(%NRND_INC)
%NRND_INC = 1
' Version 1.0
' Peter Stephensen 2002
' Helper function
sub nrnd_BoxMuller(y1 as double, y2 as double)
local x1 as double, x2 as double, w as double
do
x1 = 2 * rnd - 1
x2 = 2 * rnd - 1
w = x1 * x1 + x2 * x2
loop while w >= 1
w = sqr(-2 * log(w) / w)
y1 = x1 * w
y2 = x2 * w
end sub
' Returns a gaussian random number
' Method: Polar form of the Box-Muller transformation (see http://www.taygeta.com/random/gaussian.html)
function nrnd() as double
static flag as long
static y1 as double, y2 as double
if flag then
flag = 0
function = y2
else
flag = 1
nrnd_BoxMuller y1, y2
function = y1
end if
end function
#endif ' %NRND_INC