2

I have written this code :

for i=1:m-1
x(i+1)=mod(a*x(i)+c,m)
x(i)=x(i+1)
end

But I don't know how to write lcg as a function that generates random numbers.

LCG is a linear congruential generator which is one of the oldest and best-known pseudorandom number generator algorithms. The generator is defined by the recurrence relation:

  Xn+1 =(aXn + c) mod m

,and I want to write a function for this relation.

J.Bea
  • 51
  • 1
  • 3
  • What's LCG? What's the desired output? – Dev-iL Oct 16 '16 at 11:08
  • I have edited my question. @Dev-iL – J.Bea Oct 16 '16 at 20:23
  • What do you want different from the code you currently have? Do you simply want it in the form of a function, or do you want to make it generate the subsequent random number on each call? – beaker Oct 16 '16 at 22:09
  • Did you look in the File Exchange? Maybe you can get some hits from the following submissions (and maybe others): https://www.mathworks.com/matlabcentral/fileexchange/47426-linear-congruential-random-number-generator/content/usage_RNG.m , https://www.mathworks.com/matlabcentral/fileexchange/46129-grogono-linear-congruent-random-number-generator/content//grogono.m. – Dev-iL Oct 16 '16 at 23:30

0 Answers0