Generating Random Numbers

From Lazarus wiki
Revision as of 19:51, 10 April 2013 by Jwdietrich (talk | contribs) (Created page with "'''Random numbers''' are important resources for scientific applications, education, game development and visualization. The standard RTL function <code>[http://lazarus-c...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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 that produce random numbers of a Gaussian distribution 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;