Difference between revisions of "Generating Random Numbers"

From Lazarus wiki
Jump to navigationJump to search
Line 58: Line 58:
 
#[http://projecteuclid.org/DPubS/Repository/1.0/Disseminate?view=body&id=pdf_1&handle=euclid.aoms/1177706645 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]
 
#[http://projecteuclid.org/DPubS/Repository/1.0/Disseminate?view=body&id=pdf_1&handle=euclid.aoms/1177706645 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]
 
#Dietrich, J. W. (2002). [http://openlibrary.org/books/OL24586469M/Der_Hypophysen-Schilddrüsen-Regelkreis Der Hypophysen-Schilddrüsen-Regelkreis]. Berlin, Germany: Logos-Verlag Berlin. ISBN 978-3-89722-850-4. OCLC 50451543.
 
#Dietrich, J. W. (2002). [http://openlibrary.org/books/OL24586469M/Der_Hypophysen-Schilddrüsen-Regelkreis Der Hypophysen-Schilddrüsen-Regelkreis]. Berlin, Germany: Logos-Verlag Berlin. ISBN 978-3-89722-850-4. OCLC 50451543.
#Press, W. H., B. P. Flannery, S. A. Teukolsky, W. T. Vetterling (1989). Numerical Recipes in [[Pascal]]. The Art of Scientific Computing, Cambridge University Press, ISBN 0-521-37516-9.
+
#Press, W. H., B. P. Flannery, S. A. Teukolsky, W. T. Vetterling (1989). [http://openlibrary.org/works/OL16807779W/ Numerical Recipes in Pascal]. The Art of Scientific Computing, Cambridge University Press, ISBN 0-521-37516-9.
  
 
[[Category:Statistical algorithms]]
 
[[Category:Statistical algorithms]]

Revision as of 19:19, 13 October 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;

The same algorithm is used by the randg randg function from the RTL math unit:

function randg(mean,stddev: float): float;

Exponential Distribution

An exponential distribution occurs frequently in real-world problems. A classical example is the distribution of waiting times between independent Poisson-random events, e.g. the radioactive decay of nuclei [Press et al. 1989].

The following function delivers a single real random number out of an exponential distribution. Rate is the inverse of the mean and the constant RESOLUTION determines the granularity of gernerated random numbers.

 function randomExp(rate: real): real;
 const
   RESOLUTION = 1000;
 var
   unif: real;
 begin
   if rate = 0 then
     randomExp := NaN
   else
   begin
     repeat
       unif := random(RESOLUTION) / RESOLUTION;
     until unif <> 0;
     randomExp := -ln(unif / rate) / rate;
   end;
 end;

Poisson Distribution

See also

Functions for descriptive statistics

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.
  3. Press, W. H., B. P. Flannery, S. A. Teukolsky, W. T. Vetterling (1989). Numerical Recipes in Pascal. The Art of Scientific Computing, Cambridge University Press, ISBN 0-521-37516-9.