PartMC  2.6.1
Functions/Subroutines | Variables
pmc_rand Module Reference

Random number generators. More...

Functions/Subroutines

subroutine pmc_srand (seed, offset)
 Initializes the random number generator to the state defined by the given seed plus offset. If the seed is 0 then a seed is auto-generated from the current time plus offset. More...
 
subroutine pmc_rand_finalize ()
 Cleanup the random number generator. More...
 
real(kind=dp) function pmc_random ()
 Returns a random number between 0 and 1. More...
 
integer function pmc_rand_int (n)
 Returns a random integer between 1 and n. More...
 
integer function prob_round (val)
 Round val to floor(val) or ceiling(val) with probability proportional to the relative distance from val. That is, Prob(prob_round(val) == floor(val)) = ceil(val) - val. More...
 
integer function rand_poisson (mean)
 Generate a Poisson-distributed random number with the given mean. More...
 
integer function rand_binomial (n, p)
 Generate a Binomial-distributed random number with the given parameters. More...
 
real(kind=dp) function rand_normal (mean, stddev)
 Generates a normally distributed random number with the given mean and standard deviation. More...
 
subroutine rand_normal_array_1d (mean, stddev, val)
 Generates a vector of normally distributed random numbers with the given means and standard deviations. This is a set of normally distributed scalars, not a normally distributed vector. More...
 
integer function sample_cts_pdf (pdf)
 Sample the given continuous probability density function. More...
 
integer function sample_disc_pdf (pdf)
 Sample the given discrete probability density function. More...
 
subroutine sample_vec_cts_to_disc (vec_cts, n_samp, vec_disc)
 Convert a real-valued vector into an integer-valued vector by sampling. More...
 
character function rand_hex_char ()
 Generate a random hexadecimal character. More...
 
subroutine uuid4_str (uuid)
 Generate a version 4 UUID as a string. More...
 

Variables

integer, parameter pmc_uuid_len = 36
 Length of a UUID string. More...
 
integer, parameter pmc_rand_gsl_success = 0
 Result code indicating successful completion. More...
 
integer, parameter pmc_rand_gsl_init_fail = 1
 Result code indicating initialization failure. More...
 
integer, parameter pmc_rand_gsl_not_init = 2
 Result code indicating the generator was not initialized when it should have been. More...
 
integer, parameter pmc_rand_gsl_already_init = 3
 Result code indicating the generator was already initialized when an initialization was attempted. More...
 

Detailed Description

Random number generators.

Function/Subroutine Documentation

◆ pmc_rand_finalize()

subroutine pmc_rand::pmc_rand_finalize

Cleanup the random number generator.

Definition at line 118 of file rand.F90.

◆ pmc_rand_int()

integer function pmc_rand::pmc_rand_int ( integer, intent(in)  n)

Returns a random integer between 1 and n.

Parameters
[in]nMaximum random number to generate.

Definition at line 172 of file rand.F90.

◆ pmc_random()

real(kind=dp) function pmc_rand::pmc_random

Returns a random number between 0 and 1.

Definition at line 138 of file rand.F90.

◆ pmc_srand()

subroutine pmc_rand::pmc_srand ( integer, intent(in)  seed,
integer, intent(in)  offset 
)

Initializes the random number generator to the state defined by the given seed plus offset. If the seed is 0 then a seed is auto-generated from the current time plus offset.

Parameters
[in]seedRandom number generator seed.
[in]offsetRandom number generator offset.

Definition at line 66 of file rand.F90.

◆ prob_round()

integer function pmc_rand::prob_round ( real(kind=dp), intent(in)  val)

Round val to floor(val) or ceiling(val) with probability proportional to the relative distance from val. That is, Prob(prob_round(val) == floor(val)) = ceil(val) - val.

Parameters
[in]valValue to round.

Definition at line 214 of file rand.F90.

◆ rand_binomial()

integer function pmc_rand::rand_binomial ( integer, intent(in)  n,
real(kind=dp), intent(in)  p 
)

Generate a Binomial-distributed random number with the given parameters.

See http://en.wikipedia.org/wiki/Binomial_distribution for information on the method. The method used at present is rather inefficient and inaccurate (brute force for $np \le 10$ and $n(1-p) \le 10$, otherwise normal approximation).

For better methods, see L. Devroye, "Non-Uniform Random Variate Generation", Springer-Verlag, 1986.

Parameters
[in]nSample size.
[in]pSample probability.

Definition at line 319 of file rand.F90.

◆ rand_hex_char()

character function pmc_rand::rand_hex_char

Generate a random hexadecimal character.

Definition at line 580 of file rand.F90.

◆ rand_normal()

real(kind=dp) function pmc_rand::rand_normal ( real(kind=dp), intent(in)  mean,
real(kind=dp), intent(in)  stddev 
)

Generates a normally distributed random number with the given mean and standard deviation.

Parameters
[in]meanMean of distribution.
[in]stddevStandard deviation of distribution.

Definition at line 407 of file rand.F90.

◆ rand_normal_array_1d()

subroutine pmc_rand::rand_normal_array_1d ( real(kind=dp), dimension(:), intent(in)  mean,
real(kind=dp), dimension(size(mean)), intent(in)  stddev,
real(kind=dp), dimension(size(mean)), intent(out)  val 
)

Generates a vector of normally distributed random numbers with the given means and standard deviations. This is a set of normally distributed scalars, not a normally distributed vector.

Parameters
[in]meanArray of means.
[in]stddevArray of standard deviations.
[out]valArray of sampled normal random variables.

Definition at line 466 of file rand.F90.

◆ rand_poisson()

integer function pmc_rand::rand_poisson ( real(kind=dp), intent(in)  mean)

Generate a Poisson-distributed random number with the given mean.

See http://en.wikipedia.org/wiki/Poisson_distribution for information on the method. The method used at present is rather inefficient and inaccurate (brute force for mean below 10 and normal approximation above that point).

The best known method appears to be due to Ahrens and Dieter (ACM Trans. Math. Software, 8(2), 163-179, 1982) and is available (in various forms) from:

Unfortunately the above code is under the non-free license:

For other reasonable methods see L. Devroye, "Non-Uniform Random Variate Generation", Springer-Verlag, 1986.

Parameters
[in]meanMean of the distribution.

Definition at line 252 of file rand.F90.

◆ sample_cts_pdf()

integer function pmc_rand::sample_cts_pdf ( real(kind=dp), dimension(:), intent(in)  pdf)

Sample the given continuous probability density function.

That is, return a number k = 1,...,n such that prob(k) = pdf(k) / sum(pdf). Uses accept-reject.

Parameters
[in]pdfProbability density function (not normalized).

Definition at line 489 of file rand.F90.

◆ sample_disc_pdf()

integer function pmc_rand::sample_disc_pdf ( integer, dimension(:), intent(in)  pdf)

Sample the given discrete probability density function.

That is, return a number k = 1,...,n such that prob(k) = pdf(k) / sum(pdf). Uses accept-reject.

Parameters
[in]pdfProbability density function.

Definition at line 523 of file rand.F90.

◆ sample_vec_cts_to_disc()

subroutine pmc_rand::sample_vec_cts_to_disc ( real(kind=dp), dimension(:), intent(in)  vec_cts,
integer, intent(in)  n_samp,
integer, dimension(size(vec_cts)), intent(out)  vec_disc 
)

Convert a real-valued vector into an integer-valued vector by sampling.

Use n_samp samples. Each discrete entry is sampled with a PDF given by vec_cts. This is very slow for large n_samp or large n.

Parameters
[in]vec_ctsContinuous vector.
[in]n_sampNumber of discrete samples to use.
[out]vec_discDiscretized vector.

Definition at line 557 of file rand.F90.

◆ uuid4_str()

subroutine pmc_rand::uuid4_str ( character(len=pmc_uuid_len), intent(out)  uuid)

Generate a version 4 UUID as a string.

See http://en.wikipedia.org/wiki/Universally_Unique_Identifier for format details.

Parameters
[out]uuidThe newly generated UUID string.

Definition at line 599 of file rand.F90.

Variable Documentation

◆ pmc_rand_gsl_already_init

integer, parameter pmc_rand::pmc_rand_gsl_already_init = 3

Result code indicating the generator was already initialized when an initialization was attempted.

Definition at line 30 of file rand.F90.

◆ pmc_rand_gsl_init_fail

integer, parameter pmc_rand::pmc_rand_gsl_init_fail = 1

Result code indicating initialization failure.

Definition at line 24 of file rand.F90.

◆ pmc_rand_gsl_not_init

integer, parameter pmc_rand::pmc_rand_gsl_not_init = 2

Result code indicating the generator was not initialized when it should have been.

Definition at line 27 of file rand.F90.

◆ pmc_rand_gsl_success

integer, parameter pmc_rand::pmc_rand_gsl_success = 0

Result code indicating successful completion.

Definition at line 22 of file rand.F90.

◆ pmc_uuid_len

integer, parameter pmc_rand::pmc_uuid_len = 36

Length of a UUID string.

Definition at line 19 of file rand.F90.