B.12 Randomnumber generators
Randomnumbers are generated using a common general seed number. The seed is set to 42 every time BayES starts and is advanced appropriately every time a randomnumber generator is called. The seed can be set at any point in a script ﬁle using the statement:
Note that when a model function is called, another seed is used, which is internal to the particular model and does not advance the general seed. This allows reproducibility of results irrespective of how many random numbers, if any, have been generated in statements before the call to the model function. The model’s internal seed is also set to 42 and this value can be altered by passing an optional argument of the form:
B.12.1 Univariate distributions
The generic syntax for calling a randomnumber generator for a univariate distribution is:
where distributionName is the name of a distribution for which BayES provides random numbers (see table below), and <parameters> is a list of the distribution’s parameters, as described in section B.11. These parameters must be separated by commas if more than one parameter needs to be supplied. The size of the return matrix, W, depends on the size of the parameters passed as arguments to the function and on whether the optional arguments, M and N, are supplied:
 if the distribution’s parameters are scalars and no optional arguments are supplied then W is an $1\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}1$ matrix which contains a random draw from the respective distribution.
 if the distribution’s parameters are scalars then M and N can be used to request multiple random numbers. W in this case will be an $M\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}N$ matrix which contains random draws from the distribution. If M is supplied then N must be supplied as well.
 if the distribution’s parameters are matrices and their dimensions match (if more than one parameter needs to be supplied) then W will have the same size as the parameters and it will contain random draws from the distribution, generated by combining parameters in an elementwise fashion. Since the dimensions of W are determined by the dimensions of the distribution’s parameters, M and N must not be supplied.
 for distributions which require specifying more than one parameter, if one of the parameters supplied is a scalar and another a matrix, then the scalar parameter is expanded to match the dimensions of the matrix parameter. In this case W has the same size as the matrix parameter. Again, M and N must not be supplied because the dimensions of W are determined by the dimensions of the distribution’s parameters.
The following two tables document the randomnumber generating functions for continuous and discrete random variables, respectively.
Syntax  Arguments and performed function 
W = betarnd(alpha, beta $[$, M, N $]$);  W is a matrix of random numbers from a beta distribution with shape parameters alpha and beta.
see also betapdf, betacdf 
W = chi2rnd(p $[$, M, N $]$);  W is a matrix of random numbers from a chisquared distribution with p degrees of freedom.
see also chi2pdf, chi2cdf 
W = exprnd(lambda $[$, M, N $]$);  W is a matrix of random numbers from an exponential distribution with rate parameter lambda.
see also exppdf, expcdf 
W = evrnd(mu, sigma $[$, M, N $]$);  W is a matrix of random numbers from a typeI extremevalue distribution with location parameter mu and scale parameter sigma
see also evpdf, evcdf 
W = frnd(p1, p2 $[$, M, N $]$);  W is a matrix of random numbers from Fisher’s F distribution with numerator degrees of freedom p1 and denominator degrees of freedom p2.
see also fpdf, fcdf 
W = gamrnd(alpha, beta $[$, M, N $]$);  W is a matrix of random numbers from a gamma distribution with shape parameter alpha and rate parameter beta.
see also gampdf, gamcdf 
W = logisticrnd(mu, s $[$, M, N $]$);  W is a matrix of random numbers from a logistic distribution with mean mu and scale parameter s (variance equal to $\frac{{\mathtt{s}}^{2}{\pi}^{2}}{3}$).
see also logisticpdf, logisticcdf 
W = logitnrnd(mu, sigma $[$, M, N $]$);  W is a matrix of random numbers from a logitnormal distribution with location parameter mu and scale parameter sigma.
see also logitnpdf, logitncdf 
W = lognrnd(mu, sigma $[$, M, N $]$);  W is a matrix of random numbers from a lognormal distribution with location parameter mu and scale parameter sigma.
see also lognpdf, logncdf 
W = normrnd(mu, sigma $[$, M, N $]$);  W is a matrix of random numbers from a normal distribution with mean mu and standard deviation sigma.
see also normpdf, normcdf 
W = trnd(p $[$, M, N $]$);  W is a matrix of random numbers from a t distribution with p degrees of freedom.
see also tpdf, tcdf 
W = truncnrnd(mu, sigma $[$, M, N $]$);  W is a matrix of random numbers from a normal distribution with location parameter mu and scale parameter sigma, truncated from below at zero.
see also truncnpdf, truncncdf 
W = unifrnd($[$ M, N $]$);  W is a matrix of random numbers from a uniform distribution on the interval $\left[0,1\right]$.

W = wblrnd(alpha, beta $[$, M, N $]$);  W is a matrix of random numbers from a Weibull distribution with shape parameter alpha and scale parameter beta.
see also wblpdf, wblcdf 
Syntax  Arguments and performed function 
W = catrnd(p $[$, M, N $]$);  W is a matrix of random numbers from a categorical distribution with probability vector speciﬁed in each row of p. The number of outcomes is deduced from the number of columns of p and numbering starts at 0. That is, if p is a $1\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$ vector, the possible values of the random variable are $\left\{0,1,\dots ,k1\right\}$ and the probability of obtaining outcome $j$ is equal to the ${\left(j+1\right)}^{\text{th}}$ element of p.

W = binomrnd(n, p $[$, M, N $]$);  W is a matrix of random numbers from a binomial distribution with n trials and probability of success in each trial p.
Note that the function allows for noninteger values of n by replacing the factorials
in the expressions for the probability mass and cumulative density functions by the
Gamma function. 
W = nbinomrnd(n, p $[$, M, N $]$);  W is a matrix of random numbers from a negative binomial distribution with n failures before stopping and probability of success in each trial p.
Note that the function allows for noninteger values of n by replacing the factorials
in the expressions for the probability mass and cumulative density functions by the
Gamma function. 
W = poissonrnd(lambda $[$, M, N $]$);  W is a matrix of random numbers from a Poisson distribution with rate parameter lambda.
see also poissonpdf, poissoncdf 
B.12.2 Multivariate distributions
The following table describes statements that are used to generate random numbers from multivariate distributions. The general format of these statements is similar to randomnumber generators for univariate distributions, but, because multivariate randomnumber generators return vectors or matrices, the optional dimension arguments do not have the usual meaning: these generators can generate $M$ or one random draw per call.
Syntax  Mathematical expression  Arguments and return values 
W = mvnrnd(mu, V $[$, M $]$); 
$f\left(\mathbf{x}\right)=\frac{\mathbf{V}{}^{\frac{1}{2}}{e}^{{\left(\mathbf{x}\phantom{\rule{0.3em}{0ex}}\phantom{\rule{0.3em}{0ex}}\mu \right)}^{\prime}{\mathbf{V}}^{1}\left(\mathbf{x}\phantom{\rule{0.3em}{0ex}}\phantom{\rule{0.3em}{0ex}}\mu \right)}}{{\left(2\pi \right)}^{\frac{k}{2}}}$  W is an $M\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$ matrix of random numbers from a $k$variate normal distribution with mean mu (either $k\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}1$ or $1\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$) and variance matrix V ($k\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$).

W = wishrnd(V, p); 
$f\left(\mathbf{X}\right)=\frac{\mathbf{X}{}^{\frac{pk1}{2}}{e}^{\frac{1}{2}\mathrm{tr}\left({\mathbf{V}}^{1}\mathbf{X}\right)}}{{2}^{\frac{pk}{2}}\mathbf{V}{}^{\frac{p}{2}}{\Gamma}_{k}\left(\frac{p}{2}\right)}$  W is a $k\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$ matrix of random numbers from a $k$dimensional Wishart distribution with scale matrix V ($k\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$) and p degrees of freedom.

W = iwishrnd(V, p); 
$f\left(\mathbf{X}\right)=\frac{\mathbf{X}{}^{\frac{p+k+1}{2}}{e}^{\frac{1}{2}\mathrm{tr}\left(\mathbf{V}{\mathbf{X}}^{1}\right)}}{{2}^{\frac{pk}{2}}\mathbf{V}{}^{\frac{p}{2}}{\Gamma}_{k}\left(\frac{p}{2}\right)}$  W is a $k\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$ matrix of random numbers from a $k$dimensional inverseWishart distribution with scale matrix V ($k\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$) and p degrees of freedom.

W = drchrnd(alpha $[$, M $]$);  $f\left(\mathbf{x}\right)=\frac{1}{B\left(\alpha \right)}\prod _{j=1}^{k}{x}_{j}^{{\alpha}_{j}1}$  W is an $M\phantom{\rule{0.3em}{0ex}}\times \phantom{\rule{0.3em}{0ex}}k$ matrix of random numbers from a $k$dimensional Dirichlet distribution with concentration parameters speciﬁed in each row of alpha. The dimension of the distribution, $k\ge 2$, is inferred by the number of columns of alpha.
