Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Random numbers are usually computed using simple discrete mathematics formula. T

ID: 2247918 • Letter: R

Question

Random numbers are usually computed using simple discrete mathematics formula. The linear congruential generator is described by the formula X_n + 1 = (a X_n + c) mod m where a, c and m are parameters of the generator with X_n being the previous random number and X_n + 1 being the next random number. X_0 is referred to as the seed. This algorithm is used extensively throughout the computing world, e.g., the random number generator implemented by C and available in glibc has parametric values a = 1, 103, 515, 245 c = 12, 345 m = 2^31 - 1 Numerical random number generators are not random: they are deterministic. Given a seed, the sequence of random numbers will be the same. They are referred to as being pseudo-random because they seem to behave as randomly generated. Write a MatLab script a2task7.m that implements the above method and assigns the seed 314, 159. This script is to compute the next five random numbers and store them into variables RN1, RN2, ......., RN5.

Explanation / Answer

the linear congruential generator is a modular function, we know that there are only m possible different values of xi. And since xi is determined by xi1, we also know that the maximum cycle length of the generator is m. We will refer to this cycle length as the period of the generator. In regards to a multiplicative generator, xi1 = 0 will cause the generator to repeatedly output 0s thereafter, making the maximum period of the multiplicative congruential generator m 1.

In MATLAB, we call

LCG(1000,10)

We thus see a stream of 10,000 integers, each between 1 and 10. We now run counter on

this list, as to check that each possible number 1, 2, ...10 shows up almost an equal number

number of times. Indeed, if we run

Counter(LCG(10000,10),10)

We should get the following output:

ans = 1000 960 1022 1008 999 995 1019 956 1040 1001

function binInBigInt = binary2decimalForBigInt( longBits )
%BINARYTODECIMALFORBIGINT
randomBits = longBits;

randomBin = '';
lengthRandomBits = length(randomBits);
for i = 1:lengthRandomBits
   randomBin = strcat(randomBin, num2str(randomBits(i)));
end

binInBigInt = 0;
powerPosition = 0;
numberOfBits = lengthRandomBits;

for i = numberOfBits:-1:1
    binInBigInt = binInBigInt + str2num(randomBin(i))*2^(powerPosition);
    powerPosition = powerPosition + 1;
end

end

M = [];
    for m = 100:10000;
        M(m) = m;

    A = [];
    for a = 2:(m-1);
    A(a) = a;
    B = [];
    R = [];
    for n = 1:1000;
    R(n) = n;
    B(n) = A(a) * n;
    K = [];
            K(n)=mod(B(n),M(m));
    n=n+1;
    a=a+1;
    m=m+1;
    if K(n) == R(n)
        print (m)
        print (a)
        print ('the cycle is done')
    end

    end
end
end