Language Reference

RANDGEN Call

generates random numbers from a specified distribution

CALL RANDGEN( result, distname<, parm1><, parm2><, parm3>);
The inputs to the RANDGEN call are as follows:



result
is a matrix that is to be filled with random samples from the specified distribution.
distname
is the name of the distribution that is to be sampled.
parm1
is a distribution shape parameter.
parm2
is a distribution shape parameter.
parm3
is a distribution shape parameter.

The RANDGEN call generates random numbers by using the same numerical method as the RAND function in base SAS, with the efficiency optimized for IML. You can initialize the random number stream used by RANDGEN with the RANDSEED call. The result parameter should be preallocated to a size equal to the number of samples you want to generate. If result is not initialized, then it receives a single random sample.

The following distributions can be sampled.

Bernoulli Distribution

The random sample x is from the probability density function:

f(x) = \{ 1 & {for p=0,x=0}\    p^x(1-p)^{1-x} & {for 0\lt p\lt 1,x=0,1}\    1 & {for p=1,x=1}    .

x is in the range: x = 0,1

p is the success probability, with range: 0 \leq p \leq 1

Beta Distribution

The random sample x is from the probability density function:

f(x) = \frac{ \gamma(a+b)}{\gamma(a)\gamma(b)}x^{a-1}(1-x)^{b-1}

x is in the range: 0 \lt x \lt 1

a and b are shape parameters, with range: a \gt 0 and b \gt 0

Binomial Distribution

The random sample x is from the probability density function:

f(x) = \{ 1 & {for p=0,x=0}\    {(n\x)}p^x(1-p)^{1-x} & {for 0\lt p\lt 1,x=0, ... ,n}\    1 & {for p=1,x=1}    .

x is in the range: x = 0,1, ... ,n

p is a success probability, with range: 0 \leq p \leq 1

n specifies the number of independent trials, with range: n = 1,2, ...

Cauchy Distribution

The random sample x is from the probability density function:

f(x) = \frac{1}{\pi(1+x^2)}

x is in the range: -\infty \lt x \lt \infty

Chi-Square Distribution

The random sample x is from the probability density function:

f(x) = \frac{2^{-df/2}}{\gamma(\frac{df}2)}x^{df/2-1}e^{-x/2}

x is in the range: x \gt 0

df is degrees of freedom, with range: df \gt 0

Erlang Distribution

The random sample x is from the probability density function:

f(x) = \frac{1}{\gamma(a)}x^{a-1}e^{-x}

x is in the range: x \gt 0

a is an integer shape parameter, with range: a = 1,2, ...

Exponential Distribution

The random sample x is from the probability density function:

f(x) = e^{-x}

x is in the range: x \gt 0

F Distribution (f_{n,d})

The random sample x is from the probability density function:

f(x) = \frac{\gamma(\frac{n+d}2) n^{\frac{n}2 } d^{\frac{d}2}x^{\frac{n}2-1}}   {\gamma(\frac{n}2)\gamma(\frac{d}2)(d+n x)^{\frac{n+d}2}}

x is in the range: x \gt 0

n and d are degrees of freedom, with range: n \gt 0 and d \gt 0

Gamma Distribution

The random sample x is from the probability density function:

f(x) = \frac{x^{a-1}}{\gamma(a)}e^{-x}

x is in the range: x \gt 0

a is a shape parameter: a \gt 0

Geometric Distribution

The random sample x is from the probability density function:

f(x) = \{ (1-p)^{x-1}p & {for 0\lt p\lt 1,x=1,2, ... }\    1 & {for p=1,x=1}    .

x is in the range: x = 1,2, ...

p is the success probability, with range: 0 \lt p \leq 1

Hypergeometric Distribution

The random sample x is from the probability density function:

f(x) = \frac{{(r\x)}{(n-r\n-x)}}{{(n\n)}}

x is in the range: x = \max(0,(n-(n-r))), ... ,\min(n,r)

n is the population size, with range: n = 1,2, ...

r is the size of the category of interest, with range: r = 0,1, ... ,n

n is the sample size, with range: n = 0,1, ... ,n

Lognormal Distribution

The random sample x is from the probability density function:

f(x) = \frac{e^{-\ln^2(x)/2}}{x\sqrt{2\pi}}

x is in the range: x \geq 0

Negative Binomial Distribution

The random sample x is from the probability density function:

f(x) = \{ {(x+k-1\k-1)}(1-p)^xp^k & {for 0\lt p\lt 1,x=0,1, ... }\    1 & {for p=1,x=0}    .

x is in the range: x = 0,1, ...

p is the success probability with range: 0 \lt p \leq 1

k is an integer number that counts the number of successes, with range: k = 1,2, ...

Normal Distribution

The random sample x is from the probability density function:

f(x) = \frac{1}{\lambda\sqrt{2\pi}}\exp(-\frac{(x-\theta)^2}{2\lambda^2})

x is in the range: -\infty \lt x \lt \infty

\theta is the mean, with range: -\infty \lt \theta \lt \infty. This parameter is optional and defaults to 0.

\lambda is the standard deviation, with range: \lambda \gt 0. This parameter is optional and defaults to 1.

Poisson Distribution

The random sample x is from the probability density function:

f(x) = \frac{m^xe^{-m}}{x!}

x is in the range: x = 0,1, ...

m is the mean, with range m \gt 0

T Distribution

The random sample x is from the probability density function:

f(x) = \frac{\gamma(\frac{df+1}2)}{\sqrt{df\pi}\,\gamma(\frac{df}2)}(1+\frac{x^2}{df})^{-\frac{df+1}2}

x is in the range: -\infty \lt x \lt \infty

df is the degrees of freedom, with the range: df \gt 0

Table Distribution

The random sample i is from the probability density function:

f(i) = \{ p_i & {for i = 1,2, ... ,n}\    1-\sum_{j=1}^np_j & {for i = n+1}    .

where p is a vector of probabilities, such that 0 \leq p \leq 1, and n is the largest integer such that n \leq {size of p} and
\sum_{j=1}^n p_j \leq 1

Triangle Distribution

The random sample x is from the probability density function:

f(x) = \{ \frac{2x}h & {for 0 \leq x \leq h}\    \frac{2(1-x)}{1-h} & {for h \lt x \leq 1}    .

x is in the range: 0 \leq x \leq 1

h is the horizontal location of the peak of the triangle, with range: 0 \leq h \leq 1

Uniform Distribution

The random sample x is from the probability density function:

f(x) = 1

x is in the range: 0 \lt x \lt 1

Weibull Distribution

The random sample x is from the probability density function:

f(x) = \frac{a}{b^a}x^{a-1}e^{-(\frac{x}b)^a}

x is in the range: x \geq 0

a and b are shape parameters, with range a \gt 0 and b \gt 0

The following table describes how parameters of the RANDGEN call correspond to the distribution parameters.

Table 20.2: Parameter Assignments for Distributions
Distribution distname parm1 parm2 parm3
Bernoulli'BERNOULLI'p  
Beta'BETA'ab 
Binomial'BINOMIAL'pn 
Cauchy'CAUCHY'   
Chi-Square'CHISQUARE'df  
Erlang'ERLANG'a  
Exponential'EXPONENTIAL'   
f_{n,d}'F'nd 
Gamma'GAMMA'a  
Geometric'GEOMETRIC'p  
Hypergeometric'HYPERGEOMETRIC'nrn
Lognormal'LOGNORMAL'   
Negative Binomial'NEGBINOMIAL'pk 
Normal'NORMAL'\theta\lambda 
Poisson'POISSON'm  
T'T'df  
Table'TABLE'p  
Triangle'TRIANGLE'h  
Uniform'UNIFORM'   
Weibull'WEIBULL'ab 

In practice, distname can be in lowercase or uppercase, and you only need to specify enough letters to distinguish one distribution from the others. For example,

  
 /* generate 10 samples from a Bernoulli distribution */ 
 r = j(10,1,.); 
 call randgen(r,'ber',p);
 

Except for the normal distribution, you must specify the parameters listed for each of the preceding distributions or IML will report an error. For the normal distribution, default values of \theta=0 and \lambda=1 are used if none are supplied.

The following example illustrates the use of the RANDGEN call.

  
 call randseed(12345); 
  
 /* get four random observations from each distribution */ 
 x = j(1,4,.); 
 /* each row of m comes from a different distribution */ 
 m = j(20,4,.); 
 call randgen(x,'BERN',0.75); 
 m[1,] = x; 
 call randgen(x,'BETA',3,0.1); 
 m[2,] = x; 
 call randgen(x,'BINOM',10,0.75); 
 m[3,] = x; 
 call randgen(x,'CAUCHY'); 
 m[4,] = x; 
 call randgen(x,'CHISQ',22); 
 m[5,] = x; 
 call randgen(x,'ERLANG', 7); 
 m[6,] = x; 
 call randgen(x,'EXPO'); 
 m[7,] = x; 
 call randgen(x,'F',12,322); 
 m[8,] = x; 
 call randgen(x,'GAMMA',7.25); 
 m[9,] = x; 
 call randgen(x,'GEOM',0.02); 
 m[10,] = x; 
 call randgen(x,'HYPER',10,3,5); 
 m[11,] = x; 
 call randgen(x,'LOGN'); 
 m[12,] = x; 
 call randgen(x,'NEGB',0.8,5); 
 m[13,] = x; 
 call randgen(x,'NORMAL'); /* default parameters */ 
 m[14,] = x; 
 call randgen(x,'POISSON',6.1); 
 m[15,] = x; 
 call randgen(x,'T',4); 
 m[16,] = x; 
 p = {0.1 0.2 0.25 0.1 0.15 0.1 0.1}; 
 call randgen(x,'TABLE',p); 
 m[17,] = x; 
 call randgen(x,'TRIANGLE',0.7); 
 m[18,] = x; 
 call randgen(x,'UNIFORM'); 
 m[19,] = x; 
 call randgen(x,'WEIB',0.25,2.1); 
 m[20,] = x; 
 print m;
 

The output is as follows:

  
                        M 
  
              1         0         1         0 
              1 0.9999234 0.9842784 0.9997739 
              7         8         5        10 
      -1.209834 3.9732282 -0.048339 -1.337284 
      30.300691 20.653151 27.301922 26.878221 
      10.636299 4.6455449 7.5284821 2.5558646 
      0.2449632 2.7656037 4.2254588 0.2866158 
      0.7035829 1.2676112 0.9806787 1.4811389 
       8.475216 8.8723256 8.2993617 8.0409742 
            109         4        33        30 
              1         1         2         1 
      0.7784513 0.9792472 0.6018993 0.3643607 
              3         2         0         2 
      0.0053637 1.4026784 -0.271338 -0.416685 
              5        11         8         4 
      1.3237918 0.0505162 -0.660845 -0.634447 
              2         3         2         3 
      0.5270875 0.6909336 0.8607548 0.5450831 
      0.4064393 0.7464901 0.3463207 0.2615394 
      0.4183405 0.9981923 16.812803 0.0001131
 

Previous Page | Next Page | Top of Page