RandomNumbers

# RandomNumbers

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: stants. This generates a sequence r1 , r2 , . . . of random integers which are distributed between [0, M − 1] (if c > 0) or between [1, M ] (if c = 0). Each ri is i scaled to the interval (0, 1) by dividing by M . The parameter M is usually equal or nearly equal to the largest integer of the computer. This determines the period P of the generator and P < M . The ﬁrst number in the sequence r1 is an input and it is called the seed. If c = 0, the period of the generator is equal to M if and only if • c is relatively prime to M (ﬁnd the greatest common divisor) • a − 1 is a multiple of every prime number that divides M and • a − 1 is a multiple of 4 if M is a multiple of 4 If M > 2 is a prime number, • the maximum possible period is M − 1 and • the maximum period is achieved if a mod M = 0 and aM −1 /q mod M = 1 for every prime divisor q of M − 1. Example Consider some simple examples. Listing 1: Linear Congruential Generator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 % AM207 , RANDOM_GENERATOR % Pavlos Protopapas , Harvard University % RANDOM GENERATOR ( PLAY WITH VARIOUS c , a and M ) % clear c = 5 ; %1 a = 71*4 +1 ; M = 2^17 ; % 256 r (1) = 10 ; N =10000 ; for i =2: N ; r ( i ) = mod ( ( a * r (i -1) + c ) , M ) ; end r = r/M; rt = rand ( 1 , N ) ; k =1; for i =2:2: N /2; x ( k ) = r (2* i -1) ; xr ( k ) = rt (2* i -1) ; y ( k ) = r (2* i ) ; yr ( k ) = rt (2* i ) ; k = k +1; end ii 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 3 % VISUAL CHECK figure (1) plot (x ,y , ’b + ’) % CHECK AUTOCORR figure (2) autocorr (r ,40) % % CHECK THE MOMENTS FOR VARIOUS N VALUES clear r ; r (1) = 10; for N =2:10000 for i =2: N ; r ( i ) = mod ( ( a * r (i -1) + c ) , M ) ; end r = r/M; M1 ( N ) = mean ( r ) ; M2 ( N ) = (1/ N ) * sum ( r .^2) ; end %% figure (3) plot (10:10000 , M1 (10: end ) , ’b ’ , 10:10000 , 1/2 , ’r ’) figure (4) plot (10:10000 , M2 (10: end ) , ’b ’ , 10:10000 , 1/3 , ’r ’) Properties of Random Numbers In practice, random number generator algorithms are implemented in the...
View Full Document

## This document was uploaded on 10/07/2013.

Ask a homework question - tutors are online