Random number generators
[GridLAB-D Core]


Detailed Description

Generate random numbers according to various distributions.


Defines

#define QNAN   sqrt(-1)

Enumerations

enum  RANDOMTYPE {
  RT_INVALID = -1, RT_DEGENERATE, RT_UNIFORM, RT_NORMAL,
  RT_LOGNORMAL, RT_BERNOULLI, RT_PARETO, RT_EXPONENTIAL,
  RT_SAMPLED
}

Functions

int random_apply (char *group_expression, char *property, RANDOMTYPE type,...)
 Apply a random number to property of a group of objects.
double random_bernoulli (double p)
 Generate a Bernoulli distributed random number.
double random_degenerate (double a)
 Generate the same number always.
double random_exponential (double lambda)
 Generate an exponentially distributed random number.
int random_init (void)
double random_lognormal (double gmu, double gsigma)
 Generate a log Gaussian distributed random number.
double random_normal (double m, double s)
 Generate a Gaussian distributed random number.
double random_pareto (double m, double k)
 Generate a Pareto distributed random number.
double random_sampled (unsigned n, double *x)
 Generate a number randomly sample uniformly from a list.
int random_test (void)
 Test random distributions.
RANDOMTYPE random_type (char *name)
 Converts a distribution name to a RANDOMTYPE.
double random_uniform (double a, double b)
 Generate a uniformly distributed random number.
double random_value (RANDOMTYPE type,...)
 Generate a random value.
double randunit (void)


Enumeration Type Documentation

enum RANDOMTYPE

Enumerator:
RT_INVALID  used to flag bad random types
RT_DEGENERATE  degenerate distribution (Dirac delta function); double only_value
RT_UNIFORM  uniform distribution; double minimum_value, double maximum_value
RT_NORMAL  normal distribution; double arithmetic_mean, double arithmetic_stdev
RT_LOGNORMAL  log-normal distribution; double geometric_mean, double geometric_stdev
RT_BERNOULLI  Bernoulli distribution; double probability_of_observing_1.
RT_PARETO  Pareto distribution; double minimum_value, double gamma_scale.
RT_EXPONENTIAL  exponential distribution; double coefficient, double k_scale
RT_SAMPLED  sampled distribution; unsigned number_of_samples, double samples[n_samples]

Definition at line 13 of file random.h.


Function Documentation

int random_apply ( char *  group_expression,
char *  property,
RANDOMTYPE  type,
  ... 
)

Apply a random number to property of a group of objects.

Returns:
the number of objects changed
Parameters:
group_expression  the group definition; see find_objects()
property  the property to update
type  the distribution type

Definition at line 295 of file random.c.

References find_first(), find_next(), find_objects(), FL_GROUP, and object_set_double_by_name().

double random_bernoulli ( double  p  ) 

Generate a Bernoulli distributed random number.

The probability density function for the Bernoulli distribution is

\[ Prob\left\{x=1\right\} = 1-Prob\left\{x=0\right\} = p \]

Note that the Bernoulli distribution is a discrete distribution.

Parameters:
p  the probability of generating a 1

Definition at line 152 of file random.c.

References output_warning(), and randunit().

Referenced by random_test().

double random_degenerate ( double  a  ) 

Generate the same number always.

This is the Dirac delta function:

The probability density function for the Dirac delta function

\[ \varphi\left(a\right) = 1.0 \]

Definition at line 76 of file random.c.

References output_warning().

Referenced by random_test().

double random_exponential ( double  lambda  ) 

Generate an exponentially distributed random number.

The exponential probability density function is

\[ \varphi\left(x\right) = \left\{ \begin{array}{c c} \lambda e^{-\lambda x} & x \geq o \\ 0 & x < 0 \end{array} \right\} \]

Parameters:
lambda  the rate parameter lambda

Definition at line 237 of file random.c.

References output_warning(), randunit(), and throw_exception().

Referenced by random_test().

double random_lognormal ( double  gmu,
double  gsigma 
)

Generate a log Gaussian distributed random number.

The log-normal probability density function is

\[ \varphi\left(x\right) = \frac{1}{\sqrt{2\pi}x\sigma}e^{\frac{\left(\ln x-\mu\right)^2}{2\sigma^2}} \]

Parameters:
gmu  the geometric mean
gsigma  the geometric standard deviation

Definition at line 220 of file random.c.

References random_normal().

Referenced by random_test().

double random_normal ( double  m,
double  s 
)

Generate a Gaussian distributed random number.

The probability density function for the Gaussian (normal) distribution is

\[ \varphi\left(x\right) = \frac{1}{\sqrt{2\pi}\sigma}e^{\frac{\left(x-\mu\right)^2}{2\sigma^2}} \]

Normally distributed random numbers are generated using the Box-Muller method:

    double random_normal(double m, double s)
    {
        double r, a, b;
        do { 
            a = 2*randunit()-1;
            b = 2*randunit()-1;
            r = a*a+b*b;
        } while (r>=1);
        return sqrt(-2*log(r)/r)*a*r*s+m;
    }
Parameters:
m  the mean of the distribution
s  the standard deviation of the distribution

Definition at line 133 of file random.c.

References output_warning(), PI, and randunit().

Referenced by random_lognormal(), and random_test().

double random_pareto ( double  m,
double  k 
)

Generate a Pareto distributed random number.

The Pareto distribution is one in which the probability of drawing a number less than x is proportional to x^k. The probability density function of the Pareto distribution is

\[ \varphi\left( x<k \right) = k \frac{m^k}{x^{k+1}} x \geq m \]

The cumulative density function is

\[ \varphi\left( x<k \right) = 1-(\frac{m}{x})^k \]

.

Parameters:
m  the minimum value
k  the k value

Definition at line 198 of file random.c.

References output_warning(), randunit(), and throw_exception().

Referenced by random_test().

double random_sampled ( unsigned  n,
double *  x 
)

Generate a number randomly sample uniformly from a list.

The probability that the value a is drawn from the sample is

\[ Prob\left\{x=a\right\} = \frac{1}{n} \]

Note that the sampled distriution is a discrete distribution.

Parameters:
n  the number of samples in the list
x  the sample list

Definition at line 171 of file random.c.

References output_warning(), QNAN, randunit(), and throw_exception().

Referenced by random_test().

int random_test ( void   ) 

Test random distributions.

To run the self-test, use the --randtest command-line argument. The test results are output to the file specified by the global_testoutputfile variable.

Returns:
number of failed tests

Definition at line 404 of file random.c.

References max, min, output_test(), output_warning(), random_bernoulli(), random_degenerate(), random_exponential(), random_lognormal(), random_normal(), random_pareto(), random_sampled(), random_uniform(), and randunit().

Referenced by cmdarg_load().

RANDOMTYPE random_type ( char *  name  ) 

Converts a distribution name to a RANDOMTYPE.

Todo:
Add some other distributions (e.g., beta, gamma, Cauchy, Weibull, Laplace) (ticket #56)
Parameters:
name  the name of the distribution

Definition at line 38 of file random.c.

References RT_BERNOULLI, RT_DEGENERATE, RT_EXPONENTIAL, RT_INVALID, RT_LOGNORMAL, RT_NORMAL, RT_PARETO, RT_SAMPLED, and RT_UNIFORM.

double random_uniform ( double  a,
double  b 
)

Generate a uniformly distributed random number.

The probability density function for the uniform distribution is

\[ \varphi\left(x\right) = \left\{ \begin{array}{c c} \frac{1}{b-a} & a \leq x < b \\ 0 & x<a ; x\geq b \end{array} \right\} \]

Note that this uniform distribution includes a but does not include b.

Parameters:
a  the minimum number
b  the maximum number

Definition at line 98 of file random.c.

References output_warning(), and randunit().

Referenced by random_test().

double random_value ( RANDOMTYPE  type,
  ... 
)

Generate a random value.

Returns:
a double containing the random number
Parameters:
type  the type of distribution desired

Definition at line 318 of file random.c.


GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy