%-------------------------------------------------------------------------- % modulo_rng.m (P335: 3/05) % % Modulo random number generator % Creates a squence of psuedo-random numbers in range [0,1) using the algorithm: % x(i) = a*x(i-1) + c (mod(m)) % The maxiumum "cycle time" of this type of generator is m however a poor % choice of a, c, and m can result in a MUCH shorter cycle time as was % the case with the disaterous IBM (a=65539, c=0, m=2^31) generator. % % Here are some "good" generators (although they have relatively small m vales): % (a,c,m) = (17221,107839,510300), (4096,150889,714025), (2416,374441,1771875) % Here's an obviously "bad" generator: % (a,c,m) = (1277,0,2^15) % % This program generates a sequnce of NUM psuedo-random numbers in the array % x(i) and plots x(i) versus i. % % PRESS F5 KEY TO RUN % clear all; %Clears all variables %------------------------------------------------------------------------ %USER SET PARAMETERS (all integers): NUM=1000; %Total number of numbers generated a=29; % Define the (a,m,c)-generator: c=7; m=2^20; seed=1071; % Seed value to start generator %------------------------------------------------------------------------- % start with 1-dim array of zeros x=zeros(1,NUM); % use for loop to fill x(i) array with sequence of random numbers nold=seed; for i=1:NUM nnew=mod((a*nold+c),m); x(i)=nnew/m; nold=nnew; end figure(1); %create figure window plot(x,'.'); %plot x(i) values as points axis([0 NUM -0.5 1.5]); %sets x and y range for plot title(['a=',num2str(a),' m=',num2str(m), ' c=',num2str(c),': ',num2str(NUM), ' random numbers'])