Random
Random(dist, method, over)
Function Random generates a single random value from a probability distribution. It is not a probability distribution per se, such as Normal() or Uniform(). It generates a single value, not a sample indexed by Run, and it does so whether evaluated in a deterministic (Mid) or probabilistic context. For example.
Random(Normal(10, 2))
generates a single value generated at random from the specified Normal distribution. Random() with no parameters returns a single uniformly-distributed random number between 0 and 1.
Because there is often a need to access 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.
The full declaration of the function is
- Random(dist: Optional Unevaluated; method: Optional Scalar; over: ... Optional Index)
Optional parameters
Dist
If specified, «dist» must be a call to a distribution function that supports single-sample generation (most do, but see below). If you specify no distribution, it defaults to Uniform(0,1). If «dist» is a multivariate distribution, indexed by I
, it returns an array of random samples indexed by I
.
Method
Selects the algorithm used to generate the random number. Possible value are:
0
: use system default1
: Minimal standard2
: L'Ecuyer3
: Knuth
Over
Give it an index or list of indexes. Random will choose an independent random number for each index value, or combination of index values. It also does this if any of the other parameters is an array with one or more indexes.
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(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).
- Returns an array of independent uniform random numbers between 0 and 1 indexed by
Random(Over: I, J)
- Returns a 2-D array of independent uniform random numbers between 0 and 1, indexed by
I
andJ
. All numbers in the array are sampled independently.
- Returns a 2-D array of independent uniform random numbers between 0 and 1, indexed by
Random(Uniform(min: Array(I, J, 0), max: 1))
- This is functionally equivalent to the preceding example. It demonstrates how the «over» parameter is only a convenience, but results in an easier to interpret syntax.
Details and more examples
Distribution function support for single samples
Random supports only those distribution functions with parameter «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 method1
: use Minimal standard2
: use L'Ecuyer3
: 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)
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».
Functions supported
Random(dist) supports any of these built-in probability distributions functions as the distribution:
It also works for these distributions from the Distribution variations library:
- Beta_m_sd
- Erlang
- Gamma_m_sd
- InverseGaussian
- Lognormal_m_sd (but note that this one is superseded by LogNormal)
- Lorenzian
- NegBinomial
- Pareto
- Pert
- Rayleigh
- Smooth_Fractile
- Wald
It also works for these distributions from the Multivariate Distributions library:
Functions not supported
Random does not support these built-in distribution functions:
Enable comment auto-refresher