Main Content

mnrnd

Multinomial random numbers

Syntax

r = mnrnd(n,p)
R = mnrnd(n,p,m)
R = mnrnd(N,P)

Description

r = mnrnd(n,p) returns random values r from the multinomial distribution with parameters n and p. n is a positive integer specifying the number of trials (sample size) for each multinomial outcome. p is a 1-by-k vector of multinomial probabilities, where k is the number of multinomial bins or categories. p must sum to one. (If p does not sum to one, r consists entirely of NaN values.) r is a 1-by-k vector, containing counts for each of the k multinomial bins.

R = mnrnd(n,p,m) returns m random vectors from the multinomial distribution with parameters n and p. R is a m-by-k matrix, where k is the number of multinomial bins or categories. Each row of R corresponds to one multinomial outcome.

R = mnrnd(N,P) generates outcomes from different multinomial distributions. P is a m-by-k matrix, where k is the number of multinomial bins or categories and each of the m rows contains a different set of multinomial probabilities. Each row of P must sum to one. (If any row of P does not sum to one, the corresponding row of R consists entirely of NaN values.) N is a m-by-1 vector of positive integers or a single positive integer (replicated by mnrnd to a m-by-1 vector). R is a m-by-k matrix. Each row of R is generated using the corresponding rows of N and P.

Examples

Generate 2 random vectors with the same probabilities:

n = 1e3;
p = [0.2,0.3,0.5];
R = mnrnd(n,p,2)
R =
   215   282   503
   194   303   503

Generate 2 random vectors with different probabilities:

n = 1e3;
P = [0.2, 0.3, 0.5; ...
     0.3, 0.4, 0.3;];
R = mnrnd(n,P)
R =
   186   290   524
   290   389   321

Version History

Introduced in R2006b