| RANDMULTINOMIAL Function | 
generates a random sample from a multinomial distribution
The inputs are as follows:

is the number of desired observations sampled from the distribution.

is the number of trials for each observation.  , for
, for  .
. 
is a  vector of probabilities with
 vector of probabilities with  and
 and  .
. 
The multinomial distribution is a multivariate generalization of the binomial distribution. For each trial,  is the probability of event
 is the probability of event  , where the
, where the  are mutually exclusive and
 are mutually exclusive and  .
. 
The RANDMULTINOMIAL function returns an  matrix that contains
 matrix that contains  observations of
 observations of  random draws from the multinomial distribution. Each row of the resulting matrix is an integer vector
 random draws from the multinomial distribution. Each row of the resulting matrix is an integer vector  with
 with  . That is, for each row,
. That is, for each row,  indicates how many times event
 indicates how many times event  occurred in
 occurred in  trials.
 trials. 
If  follows a multinomial distribution with
 follows a multinomial distribution with  trials and probabilities
 trials and probabilities  , then
, then 
the probability density function for  is
 is 
|  | 
the expected value of  is
 is  .
. 
the variance of  is
 is  .
. 
the covariance of  with
 with  is
 is  .
. 
if  then
 then  is constant.
 is constant. 
if  then
 then  is Binomial
 is Binomial and
 and  is Binomial
 is Binomial .
. 
The following example generates 1000 samples from a multinomial distribution with three mutually exclusive events. For each sample, 10 events are generated. Each row of the returned matrix x represents the number of times each event was observed. The example then computes the sample mean and covariance and compares them with the expected values. Here are the code and the output:
   call randseed(1);
   prob = {0.3,0.6,0.1};
   NumTrials = 10;
   N = 1000;
   x = RANDMULTINOMIAL(N,NumTrials,prob);
   /* population mean and covariance */
   Mean = NumTrials * prob`;
   Cov = -NumTrials*prob*prob`;
   /* replace diagonal elements of Cov with Variance */
   Variance = NumTrials*prob#(1-prob);
   do i = 1 to nrow(prob);
      Cov[i,i] = Variance[i];
   end;
   SampleMean = mean(x);
   SampleCov = cov(x);
   print SampleMean Mean, SampleCov Cov;
            SampleMean                             Mean
     2.971     5.972     1.057              3        6        1
            SampleCov                               Cov
     2.0622212 -1.746559 -0.315663         2.1     -1.8     -0.3
     -1.746559 2.3775936 -0.631035        -1.8      2.4     -0.6
     -0.315663 -0.631035 0.9466977        -0.3     -0.6      0.9
For further details about sampling from the multinomial distribution, see Gentle (2003), or Fishman (1996).