### 20.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 20.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

randbinomial(100,0.4)

42 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from multinomial distribution.

The randmultinomial command finds random numbers chosen according to a multinomial distribution (see Section 20.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])

1 |  |  |  |  |  |  |  |  |  |

randmultinomial([1/2, 1/3, 1/6],["R","V","B"])

“R” |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from Poisson distribution.

Recall that given a number λ>0, the corresponding Poisson distribution P(λ) satisfies

Prob(X ≤ k)=e−λ |
λk
k!
| .

It will have mean λ and standard deviation √λ. (See also Section 20.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

randpoisson(10.6)

16 |  |  |  |  |  |  |  |  |  |

##### 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

randnorm(2,1)

3.39283224858 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from Student’s distribution.

The randstudent command finds random numbers chosen according to Student’s distribution (see Section 20.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

randstudent(5)

0.268225314184 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from χ2 distribution.

The randchisquare command finds random numbers chosen according to the χ2 distribution (see Section 20.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

randchisquare(5)

4.53970828547 |  |  |  |  |  |  |  |  |  |

##### 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 20.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

randfisher(2,3)

2.33137725333 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from gamma distribution.

The randgammad command finds random numbers chosen according to the gamma distribution (see Section 20.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

randgammad(3,1)

4.91461463472 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from beta distribution.

The randbetad command finds random numbers chosen according to the beta distribution (see Section 20.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

randbetad(2,3)

0.524453873081 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from geometric distribution.

The randgeometric command finds random numbers chosen according to the geometric distribution (see Section 20.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

randgeometric(0.2)

11 |  |  |  |  |  |  |  |  |  |

##### Generating random numbers from exponential distribution.

The randexp command finds random numbers chosen according to the exponential distribution (see Section 20.4.15).

- randexp takes λ, a positive real number (the parameter).
- randexp(λ) returns a number chosen randomly according to the exponential distribution with parameter λ.

#### Example

randexp(2.1)

0.0288626239833 |  |  |  |  |  |  |  |  |  |