18.3.3 Sampling from probability distributions
Generating random numbers from binomial distribution.
The randbinomial
command finds random numbers chosen
according to the binomial distribution (see Section 18.4.3).
-
randbinomial takes two arguments:
-
n, an integer.
- p, a probability (a number between 0 and 1).
- randbinomial(n,p) returns an integer from 0 to n chosen
randomly according to the binomial distribution with parameters n
and p; i.e., the number of successes you might get if you did an
experiment n times, where the probability of success each time is p.
Example
Generating random numbers from multinomial distribution.
The randmultinomial
command finds random numbers chosen
according to a multinomial distribution (see Section 18.4.5).
-
randmultinomial takes one mandatory and one optional argument:
-
P, a list P=[p0,…,pn−1] of n probabilities
which add to 1 (representing the probability that one of several
mutually exclusive events occurs).
- Optionally, K, a list of length n.
- randmultinomial(L) returns an index chosen randomly
according to the corresponding multinomial distribution.
- randmultinomial(L,K) returns an element of K
whose index is chosen randomly.
Examples
randmultinomial([1/2, 1/3, 1/6]) |
randmultinomial([1/2, 1/3, 1/6],["R","V","B"]) |
Generating random numbers from Poisson distribution.
Recall that given a number λ>0, the corresponding Poisson
distribution P(λ) satisfies
It will have mean λ and standard deviation √λ.
(See also Section 18.4.6.)
The randpoisson
command finds a random integer according to
a Poisson distribution.
-
randpoisson takes
λ, a positive number.
- randpoisson(λ) returns an integer chosen
randomly according to the Poisson distribution with paramter λ.
Example
Generating random numbers from normal distribution.
The randnorm
or randNorm
command chooses a random number according to a normal distribution.
-
randnorm takes two arguments:
-
µ, a real number (the mean).
- σ, a positive real number (the standard deviation).
- randnorm(µ,σ) returns a number chosen
randomly according the normal distribution with mean µ and standard
deviation σ.
Example
Generating random numbers from Student’s distribution.
The randstudent
command finds random numbers chosen according to Student’s distribution (see
Section 18.4.8).
-
randstudent takes
n, an integer (the degrees of freedom).
- randstudent(n) returns a number chosen randomly
according to Student’s distribution with n degrees of freedom.
Example
Generating random numbers from χ2 distribution.
The randchisquare
command finds random numbers chosen according to the χ2 distribution (see
Section 18.4.9).
-
randchisquare takes
n, an integer (the degrees of freedom).
- randchisquare(n) returns a number chosen randomly
according to the χ2 distribution with n degrees of freedom.
Example
Generating random numbers from Fisher-Snédécor distribution.
The randfisher command
finds random numbers chosen according to the Fisher-Snédécor distribution (see
Section 18.4.10).
-
randfisher takes two arguments:
n1 and n2, integers (degrees of freedom).
- randfisher(n1,n2) returns a number chosen randomly
according to the Fisher-Snédécor distribution with
n1 and n2 degrees of freedom.
Example
Generating random numbers from gamma distribution.
The randgammad
command finds random numbers chosen according to the gamma distribution (see
Section 18.4.11).
-
randgammad takes two arguments:
a and b, positive real numbers (the parameters).
- randgammad(a,b) returns a number chosen randomly
according to the gamma distribution with parameters a and b.
Example
Generating random numbers from beta distribution.
The randbetad
command finds random numbers chosen according to the beta distribution (see
Section 18.4.12).
-
randbetad takes two arguments:
a and b, positive real numbers (the parameters).
- randbetad(a,b) returns a number chosen randomly
according to the beta distribution with parameters a and b.
Example
Generating random numbers from geometric distribution.
The randgeometric
command finds random numbers chosen according to the geometric distribution (see
Section 18.4.13).
-
randgeometric takes
p, a probability (a number between 0 and 1).
- randgeometric(p) returns a number chosen randomly
according to the geometric distribution with probability p.
Example
Generating random numbers from exponential distribution.
The randexp command finds random numbers chosen
according to the exponential distribution (see Section 18.4.15).
-
randexp takes
λ, a positive real number (the parameter).
- randexp(λ) returns a number chosen randomly
according to the exponential distribution with parameter λ.
Example