Random Signals for Engineers using MATLAB and Mathcad Copyright 1999 Springer Verlag NY Example 2.9 Uniform Random Number Generators In this example we how we may use the linear congruential formula to generate random numbers. We will select parameters that produce acceptable and unacceptable random number sequences and show how we may detect the differences. Let us start with a set of parameters for a, c and m that produce a small periodic sequence. a=7; c=3; m=10; Since the sequence is between 0 and m-1 we expect to have or 1 to m sequences in Matlab Let us generate a two dimensional array with the row index corresponding to starting seeds of 0 to 9 z=[0:9]'; The generator formula is given below for j=1:9 for i=1:10 z(i,j+1) = mod(a*z(i,j) +c,m); end end z z = 0 1 2 3 4 5 6 7 8 9 3 0 7 4 1 8 5 2 9 6 4 3 2 1 0 9 8 7 6 5 1 4 7 0 3 6 9 2 5 8 0 1 2 3 4 5 6 7 8 9 3 0 7 4 1 8 5 2 9 6 4 3 2 1 0 9 8 7 6 5 1 4 7 0 3 6 9 2 5 8 0 1 2 3 4 5 6 7 8 9 3 0 7 4 1 8 5 2 9 6 There a three cyclic sequences, 2 of length 4 - {1 0 3 4} and {5 8 9 6} and one of length 2 - {2 7} with different starting seeds. We clearly desire a sequence where the period is long and approaching m-1 and should not exhibit periodic sequences for certain starting seeds. Let us examine a set of uniform numbers generated by the parameters a=137; c=0; m=256; x(1)=87; for j=1:256 x(j+1) = mod(a*x(j) +c,m); end Normalizing the number to the 0 - 1 interval we have u=x/256; We first draw a histogram of the number sequence and verify that it indeed conforms to a uniform density distribution. We select 5 bins of uniform width, and use the Matlab built in histogram function [nw x]= hist(u,5); bar(x,nw,.3) 60 50 40 30 20 10 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 The curve does indeed look uniform since all of the bins contain about 50 samples. We must perform other tests to verify that indeed we do not have a periodic sequence. If we plot the values ui vs. i we discover that indeed a periodic sequence exists and its period is approximately 40. plot(u) 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 50 100 150 200 250 300 Another informative plot can be made which shows the interval between adjacent numbers in the sequence. We plot ui+1 vs. ui as plot(u(2:256),u(1:255)) 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 This sequence of plots can be made for different N and the length of the sequence can be observed by increasing N until the plot does not change. This indicates that the sequences are overlaying one another as N is increased and contain repeating sequences. If we use the parameters suggested in Table 2.7-1 we find a=314159269; c=453806245; m=2^31-1; x(1)=87; for j=1:256 x(j+1) = mod(a*x(j)+c,m); end u=x/m; plot(u(2:255),u(1:254)) 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 As we increase N we see that the curve fills the space with no repeats indicating the absence of periodicity in the sequence.