Random
Function Random
The Random function generates a single random variate.
Random( ), with no parameters, returns a single uniformly-distributed random number between 0 and 1, even if it is evaluated in Mid-mode.
Declaration
Random(dist:optional unevaluated ; method:optional scalar; Over:...optional atomic)
Parameters:
- dist : If specified, must be an explicit call to a distribution function that supports single-sample generation (see below). Defaults to Uniform(0,1).
- Method : Selects the algorithm used to generate the random number. Possible value are: 0=use system default, 1=Minimal standard, 2=L'Ecuyer, 3=Knuth.
- Over: A convenient way to list indexes that independent random numbers will be generated over. (This will also occur if the index(es) occur in any of the other parameters).
Description
Random is not a distribution-function per-se, as Uniform(0,1) is. However, one often needs access to a random number generator stream, such as for rejection sampling, Metropolis-Hastings simulation, etc. Random() makes it possible to get such values, even if the global sampling method is latin hypercube, and efficiently since it isn't necessary to generate an entire sample. Random can return variates from a wide variety of distributions. It is even possible to write user-defined distribution functions for custom distributions that work with random.
Examples
Random(Uniform(-100,100)): Returns a single real-valued random number uniformly selected between -100 and 100.
Random(Uniform(1,100,integer:true)): Returns a random integer between 1 and 100 inclusive.
Random(Normal(0,1)) : Returns a single number from a standard normal distribution.
Random(Over:I): Returns an array of independent uniform random numbers between 0 and 1 indexed by I. The numbers are independent (i.e., Monte Carlo sampled, never Latin Hypercube).
Random(Over:I,J): Returns a 2-D array of independent uniform random numbers between 0 and 1, indexed by I and J. All numbers in the array are sampled independently.
Random(Uniform(min:Array(I,J,0),max:1)): This is functionally equivalent to the preceeding example. It demonstrates how the Over parameter is only a convenience, but results in an easier to interpret syntax.
Distribution Function Support for Single Samples
In order to support single sample generation, and thus be permitted as a parameter in Random, a distribution function must have a parameter named singleMethod, usually declared as:
singleSampleMethod : optional atomic numeric
When the parameter is provided, the distribution function must return a single random variate from the distribution indicated by the other parameters. Random will fill in this parameter with one of the following values, indicating which sampling method should be used:
Possible values for singleMethod: 0 = use default method 1 = use Minimal standard 2 = use L'Ecuyer 3 = use Knuth
As an example, consider what happens when Random(Normal(2,3)) is evaluated. The Random function checks that its parameter is an acceptable distribution function, and then it evaluates:
Normal(2,3,singleSampleMethod:0)
The Normal function then returns a single random variate from Normal(2,3).
Only some built-in functions currently support single variate generation. These include:
- Bernoulli
- Beta
- Binomial
- Certain
- ChiSquared
- CumDist
- Exponential
- Gamma
- Geometric
- HyperGeometric
- Logistic
- LogNormal
- Normal
- Poisson
- StudentT
- Triangular
- Uniform
- Weibull
In addition, the following distributions in the Distribution variations library can be used in Random (the Distribution Variations library must be added to your model):
- LogNormal_m_sd (but note that this one is superceded by LogNormal)
- Pert
- Gamma_m_sd
- Beta_m_sd
- Erlang
- Pareto
- Rayleigh
- Lorenzian
- NegBinomial
- InverseGaussian
- Wald
The following built-in distribution functions are not usable in Random:
User-defined functions can support single-variate generation, and therefore can be used as a parameter to Random, if they have a parameter named singleMethod.
Enable comment auto-refresher