Numbers with a Gaussian distribution: Difference between revisions

From SklogWiki
Jump to navigation Jump to search
(New page: {{Source}} Random number generators usually provide numbers with a uniform (flat) distribution. Sometimes it is desirable to generate numbers with some other distributions. The Gaussia...)
 
m (Slight tidy up.)
 
Line 1: Line 1:
{{Source}}
{{Source}}
[[Random number]] generators usually provide numbers with a uniform (flat)
[[Random_numbers |Random number generators]] usually provide numbers having a uniform (flat)
distribution. Sometimes it is desirable to generate numbers with some
distribution. However, sometimes it is desirable to generate numbers with some
other distributions. The Gaussian (normal) distribution is of paramount imporance.
other distributions. For example, the [[Gaussian distribution |Gaussian (normal) distribution]] is of paramount importance.
==Fortran 90 implementation==
==Fortran 90 implementation==
This Fortran 90 implementation is adapted from Ref. 1, based on an algorithm from the Numerical Recipes collection, Ref. 2. The function '''ran()''' calls a [[Random_numbers | randon number generator]]:
This Fortran 90 function is adapted from Ref. 1, based on an algorithm from the Numerical Recipes collection (Ref. 2). The function '''ran()''' calls a random number generator:
<small><pre>
<small><pre>
! Returns random numbers distributed following a Gaussian with
! Returns random numbers distributed following a Gaussian with
Line 35: Line 35:
==References==
==References==
# Daan Frenkel and Berend Smit "Understanding Molecular Simulation: From Algorithms to Applications" p. 411 Academic Press (1996)
# Daan Frenkel and Berend Smit "Understanding Molecular Simulation: From Algorithms to Applications" p. 411 Academic Press (1996)
# [http://www.nr.com Numerical Recipes]
# [http://www.nr.com Numerical Recipes (Third Edition) website ]


[[category: random numbers]]
[[category: random numbers]]
[[category: computer simulation techniques]]
[[category: computer simulation techniques]]

Latest revision as of 13:31, 13 November 2007

This page contains computer source code. If you intend to compile and use this code you must check for yourself the validity of the code. Please read the SklogWiki disclaimer.

Random number generators usually provide numbers having a uniform (flat) distribution. However, sometimes it is desirable to generate numbers with some other distributions. For example, the Gaussian (normal) distribution is of paramount importance.

Fortran 90 implementation[edit]

This Fortran 90 function is adapted from Ref. 1, based on an algorithm from the Numerical Recipes collection (Ref. 2). The function ran() calls a random number generator:

! Returns random numbers distributed following a Gaussian with
! unit variance

function gauss()

  implicit none

  real gauss

  real v1,v2,r

  real ranmar

  do
     v1=2.0*ranmar()-1.0
     v2=2.0*ranmar()-1.0
     r=v1*v1+v2*v2
     if(r.lt.1.0) exit
  enddo

  gauss=v1*sqrt(-2.0*log(r)/r)

  return

end function gauss

References[edit]

  1. Daan Frenkel and Berend Smit "Understanding Molecular Simulation: From Algorithms to Applications" p. 411 Academic Press (1996)
  2. Numerical Recipes (Third Edition) website