2

I am making another python script to perform linear congruential generator to generate 55 pseudo-random number but having trouble understanding the algorithm of linear congruential generator and how does my scripts work even though the script is simple for me to write.

Based on the python script, why is the output is changing in every loop? Is the python script correct for the linear congruential generator?

#!/usr/bin/python3
# Fix variable for the seed, modulus, a and c 
seed = 8 
a = 2
c = 2 
m = 20

# counter for how many iterations we've run
counter = 0
#Perfom number of iterations requested by user
while counter < 50:
    # Store value of each iteration
    seed = (a * seed + c) % m
    counter = counter + 1
  • 1
    Because the value of seed is getting update in each iteration. Seems like it is fine based off here https://en.wikipedia.org/wiki/Linear_congruential_generator – Yoshikage Kira May 24 '21 at 00:41
  • What did you expect? What would be the point if the output never changed? – President James K. Polk May 25 '21 at 01:17
  • Yes I am aware that the output would change. but I made a mistake in the code. let assume i got a random number which is 19 and I plus 11 which corresponds to the plaintext character, L and it became 30. There is no such thing as 30 in alphabet as there are only 26 . – bully_programming May 25 '21 at 11:07

1 Answers1

1

The output is changing on every iteration due to the seed variable being updated, your using the seed from the previous iteration to change the seed of the next iteration.

seed = (a * seed + c) % m

On the first iteration your seed is 5, so

(3 * 5 + 3) % 19

gives 18. On the second iteration

(3 * 18 + 3) % 19

gives 0, because 3*18 + 3 is 57 and 57 modulus m which is 19 is 0. and so the loop continues, with each previous seed impacting the next.