Write a MATLAB script to implement the linear congruential algorithm to generate
ID: 1846904 • Letter: W
Question
Write a MATLAB script to implement the linear congruential algorithm to generate 5000 pseudo-random variates in the interval (0,1). Use the following parameters: seed=17; A=1103515245; M=2147483648; C=12345.Obtain the sample mean of generated numbers using the MATLAB function mean.
Parameters The parameters M, C, and A in the linear congruential generator are chosen to make the sequence as random as possible. Choices for these values must meet the following necessary and sufficient conditions contained in the theorem proved by Hull Dobell (1962). 1. C must be relatively prime to M (that is, their greatest common divisor is 1) 2. B = A-1 is a multiple of p, for every prime p that divides M 3. B is a multiple of 4 if M is a multiple of 4. Write a MATLAB script to implement the linear congruential algorithm to generate 5000 pseudo-random variates in the interval (0,1). Use the following parameters: seed=17; A=1103515245; M=2147483648; C=12345.
Obtain the sample mean of generated numbers using the MATLAB function mean.
Parameters The parameters M, C, and A in the linear congruential generator are chosen to make the sequence as random as possible. Choices for these values must meet the following necessary and sufficient conditions contained in the theorem proved by Hull Dobell (1962). 1. C must be relatively prime to M (that is, their greatest common divisor is 1) 2. B = A-1 is a multiple of p, for every prime p that divides M 3. B is a multiple of 4 if M is a multiple of 4. 1. C must be relatively prime to M (that is, their greatest common divisor is 1) 2. B = A-1 is a multiple of p, for every prime p that divides M 3. B is a multiple of 4 if M is a multiple of 4.
Explanation / Answer
Here is the code:
function [row]=lcg(a, c, m, x, sample)
% Linear Congruential Generators
% x the initial seed, 0 <= c < m
% a the multiplier, 0 <= a < m, normally greater than 1
% c the increment 0 <= c < m
% m the modulus, prime numbers are best
xseq=x;
for j=1:sample % generate m+5 integers
x= rem((a*x+c),m); % (a*x + c) mod m;
xseq=[xseq;x]; % concatenate numbers, in a cloumn
row=xseq'; % transpose to a row
end
row = row/m;
% calculate the real number of a period p
average_val = mean(row); % average
dispval = ['Average value is:: ',num2str(average_val)];
disp(dispval);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.