Difference between revisions of "Generating Random Numbers"
From Lazarus wiki
Jump to navigationJump to searchJwdietrich (talk | contribs) |
Jwdietrich (talk | contribs) |
||
Line 5: | Line 5: | ||
== Normal (Gaussian) Distribution == | == Normal (Gaussian) Distribution == | ||
− | One of the more common algorithms to produce normally distributed random numbers from uniformly distributed random numbers is the Box-Müller approach. The following function calculates Gaussian-distributed random numbers: | + | One of the more common algorithms to produce normally distributed random numbers from uniformly distributed random numbers is the [http://en.wikipedia.org/wiki/Box–Muller_transform Box-Müller approach]. The following function calculates Gaussian-distributed random numbers: |
<code> | <code> |
Revision as of 21:00, 10 April 2013
Random numbers are important resources for scientific applications, education, game development and visualization.
The standard RTL function random
generates random numbers that fulfill a uniform distribution. In order to create random numbers of other distributions special algorithms are necessary.
Normal (Gaussian) Distribution
One of the more common algorithms to produce normally distributed random numbers from uniformly distributed random numbers is the Box-Müller approach. The following function calculates Gaussian-distributed random numbers:
function rnorm (mean, sd: real): real;
{Calculates Gaußian random numbers according to the Box-Müller approach}
var
u1, u2: real;
begin
u1 := random;
u2 := random;
rnorm := mean * abs(1 + sqrt(-2 * (ln(u1))) * cos(2 * pi * u2) * sd);
end;