jeudi 29 juillet 2021

Pseudorandom number generator

I want to write a Python program that generates random numbers. I have already tried the following

import random
from random import randrange
a = randrange(9999)
print('Multiplier a is: ',a)
m = randrange(9999)
print('Modulus m is: ',m)
c = randrange(9999)
print('Increment c is: ',c)
x0 = randrange(9999)
print('Seed x0 is: ',x0)
i = 0
while i<1000:
   x1 = (a*x0 + c) %m
   x0 = x1
   i+=1
   print(x1)

The program works, but my problem is that i do not know how to stop the sequence if i get the same random numbers x_n=x_m. Also, is there some way to save the resulting numbers to a file?




Aucun commentaire:

Enregistrer un commentaire