0

I implemented a simple code for a linear congruential generator

clear all; clc

% Input
m = 59;         % module
a = 17;         % multiplier
c = 43;         % increase
X0 = 27;        % seed

n = 100;        % sample length

y = [X0 zeros(1,n-1)];

% recursive formula
% X(n+1) = (a*X(n) + c) mod m

 for i = 2:n

  y(i) = mod(a*y(i-1)+c,m);

end

x = 0:1:n-1;

%for i = 1:n

 %   plot(x,y);

%end

What I would like to do is a plot where each time the period repeats it draws a vertical line upward as in this graph

Cyclic graph with vertical lines at cycle points

I think I have to use the plot function inside a FOR loop and an IF-ELSE to see if the value of the subsequence X(n) is equal to the seed X(0) but I have no idea how to implement it

I think I have to use the plot function inside a FOR loop and an IF-ELSE to see if the value of the subsequence X(n) is equal to the seed X(0) but I have no idea how to implement it

SimoPape
  • 13
  • 2
  • Specifying the language would be useful. Also, you don't seem to be having any sort of problem with the LCG, you're not asking a probability question, and you're not really doing Monte Carlo (using random sampling to address a complex system's behavior via statistical estimation). You're just iterating through a cyclic function. Looks to me your question is really about plotting, and none of the tags you've used are relevant. – pjs Jan 05 '23 at 20:07
  • The language is matlab. I have included the Monte Carlo tag because I need to make a comparison between computing an integral (with Monte Carlo) using matlab's "rand" function and the LCG algorithm – SimoPape Jan 05 '23 at 20:14
  • Yes as the title says I am trying to plot a graph of the succession generated by the LCG @pjs – SimoPape Jan 05 '23 at 20:16
  • 1) Add a tag for `matlab` to your question then. That's relevant, and far more likely to get you the assistance you want. 2) *This* question does not have anything to do comparing Monte Carlo integrals based on different PRNGs. Concepts that apply to a different question you plan to work towards are not relevant to this question. – pjs Jan 05 '23 at 20:21
  • Ok thank you for all the notes if you also have a solution for my problem I would be grateful otherwise thank you anyway @pjs – SimoPape Jan 05 '23 at 20:55
  • Not a Matlab user, so I can't help you with your plotting problem. I would recommend you read the [How to Ask](https://stackoverflow.com/help/how-to-ask) guidance from the help center though. – pjs Jan 05 '23 at 21:01

0 Answers0