34.5 - Simulating Random Numbers

34.5 - Simulating Random Numbers

In statistical research, it is a rather common practice to generate (i.e., "simulate") numbers that follow some underlying probability distribution. Fortunately, SAS has several random number generator functions available to simulate random phenomena with certain probability distributions. We'll take a quick look at just three of the possible functions here. The others that are available in work similarly to these here.

Example 34.18

The following program uses the rannor( ) function to generate 200 random normal variables with mean 140 and standard deviation 20:

	 DATA simula1;
	do i = 1 to 200;
		x = 140 + 20*rannor(3452083);
		output;
	end;
RUN;

PROC UNIVARIATE data=simula1 plot;
	title1 'Simulated Normal Variate';
	title2 'with Mean 140 and Standard Deviation 20';
	var x;
RUN;

The rannor( ) function returns a (pseudo) random number from a standard normal distribution with mean 0 and standard deviation 1. The x= assignment statement modifies the random number so that it comes from a normal distribution with mean 140 and standard deviation 20. The OUTPUT statement must be used to dump the random number after each iteration of the DO loop. If the OUTPUT function is not present, you would end up with only one random number, namely the last one generated. Incidentally, the rannor( ) function is an alias for the normal( ) function.

Launch and run the SAS program, so you can review the output from the UNIVARIATE procedure. You should see a stem-and-leaf plot, a boxplot, and a normal probability plot that should make it believable that the data arose from a normal distribution. You might also want to check out the sample mean and sample standard deviation to see how (impressively) close they are to 140 and 20, respectively, with a sample of just 200 observations.

Example 34.19

The following program uses the ranbin(seed, n, p) function to generate a random sample of 20 observations from a binomial distribution with n = 20 and p = 0.5:

DATA simula2;
	do i = 1 to 20;
       b = ranbin(2340234,20,0.5);
       output;
    end;
 RUN;

 PROC UNIVARIATE data=simula2 plot;
    title1 'Simulated Binomial Variate';
    title2 'with n = 20 and p = 0.5';
    var b;
 RUN;

Launch and run the SAS program, so you can review the output from the UNIVARIATE procedure. You might want to check out the sample mean and sample standard deviation to see how (impressively) close they are to 10 (np) and 2.24 (square root of np(1-p)), respectively, with a sample of just 20 observations.

Example 34.20

The following program uses the ranpoi(seed, mean) function to generate a random sample of 200 observations from a Poisson distribution with a mean of 4:

DATA simula3;
	do i = 1 to 200;
		p = ranpoi(67, 4);
		output;
	end;
RUN;

PROC UNIVARIATE data=simula3 plot;
	title 'Simulated Poisson Variate with Mean 4';
	var p;
RUN;

Launch and run the SAS program, so you can review the output from the UNIVARIATE procedure. You might want to check out the sample mean and sample standard deviation to see how (impressively) close they are to 4 and 2, respectively, with a sample of just 200 observations.


Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility