MODULE random_seq_mod¶
Overview¶
Provides access to any number of reproducible random sequences. Can sample from uniform, gaussian, two-dimensional gaussian, gamma, inverse gamma, and exponential distributions.
The current random sequence generator is a Fortran version of the GNU Library implementation of the Mersenne Twister algorithm. The original code is in the C language and the conversion to Fortran was done by the DART team.
There are test programs in the developer_tests/random_seq
directory which show examples of calling these routines.
Build and run these tests in the test
subdirectory.
Other modules used¶
types_mod
utilities_mod
Public interfaces¶
use random_seq_mod, only : |
random_seq_type |
init_random_seq |
|
random_uniform |
|
random_gaussian |
|
several_random_gaussians |
|
twod_gaussians |
|
random_gamma |
|
random_inverse_gamma |
|
random_exponential |
A note about documentation style. Optional arguments are enclosed in brackets [like this].
type random_seq_type
private
integer :: mti
integer(i8) :: mt(624)
real(r8) :: lastg
logical :: gset
end type random_seq_type
This type is used to uniquely identify a sequence. Keeps the state history of the linear congruential number generator. In this implementation it is based on the Mersenne Twister from the GNU Scientific Library.
call init_random_seq(r, [, seed])
type(random_seq_type), intent(inout) :: r
integer, optional, intent(in) :: seed
Initializes a random sequence for use. This must be called before any random numbers can be generated from this sequence. Any number of independent, reproducible random sequences can be generated by having multiple instances of a random_seq_type. A specified integer seed, optional, can produce a specific ‘random’ sequence.
|
A random sequence type to be initialized. |
|
A seed for a random sequence. |
var = random_uniform(r)
real(r8) :: random_uniform
type(random_seq_type), intent(inout) :: r
Returns a random draw from a uniform distribution on interval [0,1].
|
A random draw from a Uniform[0,1] distribution. |
|
An initialized random sequence type. |
var = random_gaussian(r, mean, standard_deviation)
real(r8) :: random_gaussian
type(random_seq_type), intent(inout) :: r
real(r8), intent(in) :: mean
real(r8), intent(in) :: standard_deviation
Returns a random draw from a Gaussian distribution with the specified mean and standard deviation.
See this Wikipedia page for more explanation about this function.
|
A random draw from a gaussian distribution. |
|
An initialized random sequence type. |
|
Mean of the gaussian. |
|
Standard deviation of the gaussian. |
call several_random_gaussians(r, mean, standard_deviation, n, rnum)
type(random_seq_type), intent(inout) :: r
real(r8), intent(in) :: mean
real(r8), intent(in) :: standard_deviation
integer, intent(in) :: n
real(r8), dimension(:), intent(out) :: rnum
Returns n
random samples from a gaussian distribution with the specified mean and standard deviation. Array
rnum
must be at least size n
.
|
An initialized random sequence type. |
|
Mean of the Gaussian to be sampled. |
|
Standard deviation of the Gaussian. |
|
Number of samples to return |
|
The random samples of the Gaussian. |
call twod_gaussians(r, mean, cov, rnum)
type(random_seq_type), intent(inout) :: r
real(r8), dimension(2), intent(in) :: mean
real(r8), dimension(2,2), intent(in) :: cov
real(r8), dimension(2), intent(out) :: rnum
Returns a random draw from a 2D gaussian distribution with the specified mean and covariance.
The algorithm used is from Knuth, exercise 13, section 3.4.1. See this Wikipedia page for more explanation about this function.
|
An initialized random sequence type. |
|
Mean of 2D gaussian distribution. |
|
Covariance of 2D gaussian. |
|
Returned random draw from gaussian. |
var = random_gamma(r, rshape, rscale)
real(r8) :: random_gamma
type(random_seq_type), intent(inout) :: r
real(r8), intent(in) :: rshape
real(r8), intent(in) :: rscale
Returns a random draw from a Gamma distribution with specified rshape
and rscale
. Both must be positive.
Note that there are three different parameterizations in common use:
With shape parameter κ (kappa) and scale parameter θ (theta).
With shape parameter α (alpha) and rate parameter β (beta). Alpha is the same as kappa, and beta is an inverse scale parameter so β = 1/θ.
With shape parameter κ (kappa) and mean parameter μ (mu). μ = κ/β, so β = κ/μ.
This form uses the first parameterization, shape (κ) and scale (θ). The distribution mean is κθ and the variance is κ(θ²). This routine is based on the Gamma(a,b) generator from the GNU Scientific library. See this Wikipedia page for more explanation of the various parameterizations of this function.
|
A random draw from a gamma distribution. |
|
An initialized random sequence type. |
|
Shape parameter. Often written as either alpha or kappa. |
|
Scale parameter. Often written as theta. If you have a rate parameter (often beta) pass in (1/rate) for scale. |
var = random_inverse_gamma(r, rshape, rscale)
real(r8) :: random_inverse_gamma
type(random_seq_type), intent(inout) :: r
real(r8), intent(in) :: rshape
real(r8), intent(in) :: rscale
Returns a random draw from an inverse Gamma distribution with the specified shape
and scale
. Both must be
positive. If you have ‘rate’ instead of ‘scale’ pass in (1/rate) for scale.
See this Wikipedia page for more explanation about this function.
|
A random draw from an inverse gamma distribution. |
|
An initialized random sequence type. |
|
Shape parameter. Often written as either alpha or kappa. |
|
Scale parameter. Often written as theta. If you have a rate parameter (often beta) pass in (1/rate) for scale. |
var = random_exponential(r, rate)
real(r8) :: random_exponential
type(random_seq_type), intent(inout) :: r
real(r8), intent(in) :: rate
Returns a random draw from an exponential distribution with the specified rate
. If you have a scale parameter
(which is the same as the mean, the standard deviation, and the survival parameter), specify (1/scale) for rate.
See this Wikipedia page for more explanation about this function.
|
A random draw from an exponential distribution. |
|
An initialized random sequence type. |
|
Rate parameter. Often written as lambda. If you have a scale parameter pass in (1/scale) for rate. |
Namelist¶
This module has no namelist input.
Files¶
NONE
References¶
Knuth, Vol 2.
Private components¶
init_ran |
|
ran_unif |
|
ran_gauss |
|
ran_gamma |
call init_ran(s, seed)
type(random_seq_type), intent(out) :: s
integer, intent(in) :: seed
Initializes a random sequence with an integer. Any sequence initialized with the same integer will produce the same sequence of pseudo-random numbers.
|
A random sequence to be initialized |
|
An integer seed to start the sequence. |
var = ran_unif(s)
real(r8) :: ran_unif
type(random_seq_type), intent(inout) :: s
Generate the next uniform [0, 1] random number in the sequence.
|
Next uniformly distributed [0, 1] number in sequence. |
|
A random sequence. |
var = ran_gauss(s)
real(r8) :: ran_gauss
type(random_seq_type), intent(inout) :: s
Generates a random draw from a standard gaussian.
|
A random draw from a standard gaussian. |
|
A random sequence. |
var = ran_gamma(r, rshape, rscale)
real(r8) :: ran_gamma
type(random_seq_type), intent(inout) :: r
real(r8), intent(in) :: rshape
real(r8), intent(in) :: rscale
Generates a random draw from a Gamma distribution. See notes in the random_gamma() section about (alpha,beta) vs (kappa,theta) vs (kappa,mu) parameterizations. This is transcribed from C code in the GNU Scientific library and keeps the (shape,scale) interface.
|
A random draw from a Gamma distribution. |
|
A random sequence. |
|
Shape parameter. |
|
Scale parameter. (This is the inverse of a rate parameter.) |