randvar (alias: random_variable) takes a probability distribution specification as its argument and returns an object representing a random variable. Its value(s) can be generated subsequently by calling sample, rand, randvector or randmatrix.
The probability distribution is specified as a sequence of arguments. The supported types are : uniform, normal, binomial, multinomial, negbinomial, Poisson, Student, FisherSnedecor, Cauchy, Weibull, beta, gamma, chisquare, geometric, exponential and discrete.
The usual way to specify a continuous distribution is to pass the probability density function as the first argument, followed by one or more (numeric) parameters. However, it can also be defined by specifying its type and first and/or second moment (the mean and/or the standard deviation/variance); the supported types are : normal, uniform, binomial, Poisson, geometric, exponential, gamma, beta and Weibull. Additionally, a uniform distribution can be defined by specifying its range as an interval. The arguments are entered in form:
To create a discrete random variable one can pass either
The weights are automatically scaled by the inverse of their sum to obtain the values of the probability mass function. If a function f is given instead of a list of weights, then w_{k}=f(a+k) for k=0,1,…,b−a unless N is given, in which case w_{k}=f(x_{k}) where x_{k}=a+(k−1) b−a/N and k=1,2,…,N. The resulting random variable X has values in {0,1,…,n−1} for 0based modes (e.g. Xcas) resp. in {1,2…,n} for 1based modes (e.g. Maple). If the list V of custom objects is given, then V[X] is returned instead of X. If N is given, then v_{k}=x_{k} for k=1,2,…,N.
To define a random variable with a FisherSnedecor distribution (two degrees of freedom), input :
Output :
To generate some values of X, input :
Output :
Input :
Output :
To define a random variable with multinomial distribution, input :
Output :
Input :
Output :
Some continuous distributions can be defined by specifying its first and/or second moment. Input :
Output :
Input :
Output :
Input :
Output :
Input :
Output :
Input :
Output :
Input :
Output :
Input :
Output :
Input :
Output :
Uniformly distributed random variables can be defined by specifying the support as an interval. Input :
Output :
Input :
Output :
The following examples demonstrate various ways to define a discrete random variable. Input :
Output :
Input :
Output :
Input :
Output :
Input :
^
2,range=10..10):;Output :
Input :
Output :

Discrete random variables can be used to approximate custom continuous random variables. For example, consider a probability density function f as a mixture of two normal distributions on the support S=[−10,10]. We sample f in N=10000 points in S. Input :
Now we generate 25000 values of X and plot a histogram :
Output :
Sampling from discrete distributions is fast : generating 25 million samples from the distribution of X which has about 10000 outcomes takes only couple of seconds. In fact, the sampling complexity is constant. Also observe that the process isn’t slowed down by spreading it across 1000 calls of randvector. Input :
Evaluation time: 2.12
Independent random variables can be combined in an expression, yielding a new random variable. In the example below, we define a lognormally distributed variable Y from a variable X with standard normal distribution. Input :
Output :
It is known that E[Y]=e^{µ+σ2/2}. The mean of L should be close to that number. Input :
^
2/2)
Output:
In case a compound random variable is defined as an expression containing several independent random variables X,Y,… of the same type, it is sometimes needed to prevent its evaluation when passing it to randvector or randmatrix. Input :
X/Y is wrapped by eval because otherwise it would automatically reduce to 1 as X and Y are both normald(0,1). Input :
Output :
To save typing, one can define Z with eval(∗,0) and pass eval(Z,1) to randvector or randmatrix. Input :
Output :
Parameters of a distribution can be entered as symbols to allow (re)assigning them at any time. Input :
Now execute the following command line several times in a row. The parameter λ is updated in each iteration :
Output (by executing the above command line three times) :