MATH 493 Practice Exercises - Random Processing

Basic Random Number Generation - Uniform and Gaussian

MATLAB has several tools for generating random numbers. The most commonly used are rand (uniform distribution between 0 and 1) and randn (normal/Gaussian distribution, mean 0 and variance 1).

Uniform Random Samples

% Generate a 4x4 MATRIX of random numbers, all between 0 and 1  
A = rand(4);  
 
% Generate 4 random (uniform numbers)  
v = rand(4, 1);  
 
% Generate 1000 random numbers and plot them  
v = rand(1000,1);  
plot(v, '.’);  
 
% Show the histogram, which should be (roughly) uniform  
hist(v, 10);  
 
% Increase N, and the histogram should look more (relatively) uniform  
v = rand(100000, 1)  
hist(v, 10);

Gaussian/Normal Random Samples

% Generate 1000 Gaussian samples  
v = randn(1000, 1);  
hist(v, 40)  
 
% Compare with 100,000 samples  
v = randn(100000, 1);  
hist(v, 40)  
 
% Overlay the target normal curve  
N = 10000;  
v = randn(N, 1);  
dx = 0.1;  
hist(v, [-5:dx:5]);  
x = linspace(-4, 4);  
y = (N*dx)* 1/sqrt(2*pi) * exp(-x.^2/2);  
hold on;  
plot(x, y, 'color’, ’red’, ’linewidth’, 2);  
hold off;

Repeatable Randomness - Streams

When doing testing with randomness, it ironically helps to (sometimes) be able to re-create exactly the effect you observed on one run. To do this, you need to be able to re-start the sequence of pseudo-random value coming out of the random number stream.

MATLAB by default uses one default stream. It has many more options, though, primarily due to possible parallelization of simulation code. Random-related simulations only benefit from the randomness if each simulation is run from an independent random number generator. For any sequential program, one random number stream is all you will need.

The streams for random numbers are structures/objects in MATLAB, so typically use the class.function or class.value syntax.

% Display information about the current stream  
RandStream.getDefaultStream  
 
% Create a new stream, with a specific seed value  
s = RandStream.create('mt19937ar’, ’Seed’, 1209587);  
RandStream.setDefaultStream(s);  
 
% Simulate two sequences back-to-back from the new stream  
v1 = randn(100, 1);  
v2 = randn(100, 1);  
plot(v1, v2, '.’);  
fprintf('Max difference in v1, v2: %.5g\n’, max(abs(v1-v2)));  
fprintf('Correlation between v1, v2:  %.5g\n’, corr(v1, v2));  
 
% Now simulate, reset the stream, then re-simulate plot:  
s = RandStream.create('mt19937ar’, ’Seed’, 1209587);  
RandStream.setDefaultStream(s);  
v1 = randn(100, 1);  
 
s = RandStream.create('mt19937ar’, ’Seed’, 1209587);  
RandStream.setDefaultStream(s);  
v2 = randn(100, 1);  
plot(v1, v2, '.’);  
fprintf('Max difference in v1, v2: %.5g\n’, max(abs(v1-v2)));  
fprintf('Correlation between v1, v2:  %.5g\n’, corr(v1, v2));

Other Resources

Describing the Random Number Generators in MATLAB

There is good discussion of the random number generators in MATLAB in Chapter 9 of Moler's textbook, available on-line:

http://www.mathworks.com/moler/random.pdf

Describing the Random Number Generators in MATLAB
Another common need in simulation is correlated normal variates. There are several ways to generate sequences where the values are correlated, and one reasonable way I found on-line is available here.

http://www.sitmo.com/doc/Generating\_Correlated\_Random\_Numbers