0

hi im having trouble creating a linear congruential generator in MatLab, the ones that I found online work quite different than mine. then im trying to print values of the m and a (relatively prime, m being a large prime obviously) and check when the cycle is full. I know all the math stuff, im getting used to matlab and its hard to implement this for me even though i should know. my program looks like this:

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

also im not too familiar with MatLab so im probably creating arrays the wrong way. thanks in advance.

  • So how exactly does your output not match what you expect? Can you elaborate? Does it run at all? – Phonon Mar 15 '12 at 17:49
  • it doesnt run at all, i know what i wanna do i cant implement it in matlab. i want to create a matrix with column A, M, B(which is A*n(seed)) and BmodM. then want to compare the BmodMs to n(seed). if theyre equal i need to get all the values of M and A. if not n=n+1, a=a+1, m=m+1. – lopiu lipio Mar 15 '12 at 18:15

1 Answers1

0

Well you aren't really asking a question there. Here is some advice for you:

1) Pre-allocate the matrices: M = zeros(9900,1), A = zeros(9998,1), you will get much faster results when you loop, or even better M = 100:10000 works directly if the values you want to put in are as simple as that.

2) You do not need to do the a = a+1, the for loop does it automatically for you (unless it's there for another reason I'm unaware of).

Smash
  • 3,722
  • 4
  • 34
  • 54
  • R = zeros(10000,10000); n = zeros(5000,1); M = zeros(5000,1); for m = 150:5000; M(m) = m; a = zeros(m,1); for n = 1:5000; R(n) = n; B = zeros(10000,1); for a = 2:m-1; B = (a*R); K = zeros(10000,1); K(B,M)=mod(B,M); if K(B,M) == Ro print (M) print (a) print ('the cycle is done') end end end end – lopiu lipio Mar 15 '12 at 21:01