Difference between revisions of "Generating Random Numbers"

From Free Pascal wiki
Jump to navigationJump to search
m (→‎Normal (Gaussian) Distribution: We're not in Germany :))
Line 9: Line 9:
 
<code>
 
<code>
 
  function rnorm (mean, sd: real): real;
 
  function rnorm (mean, sd: real): real;
  {Calculates Gaußian random numbers according to the Box-Müller approach}
+
  {Calculates Gaussian random numbers according to the Box-Müller approach}
 
   var
 
   var
 
   u1, u2: real;
 
   u1, u2: real;

Revision as of 14:24, 11 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. Uniformly distributed random numbers are not useful for every application. 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 Gaussian 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;

Exponential Distribution

Poisson Distribution

References

  1. G. E. P. Box and Mervin E. Muller, A Note on the Generation of Random Normal Deviates, The Annals of Mathematical Statistics (1958), Vol. 29, No. 2 pp. 610–611
  2. Dietrich, J. W. (2002). Der Hypophysen-Schilddrüsen-Regelkreis. Berlin, Germany: Logos-Verlag Berlin. ISBN 978-3-89722-850-4. OCLC 50451543.