lundi 1 novembre 2021

Linear Congruential Random Number Generator in Python

I'm asked to write a function to generate random integers using the Pseudorandom relation formula (xi+1) = (a*xi + c) mod M where a, c and M are integer constants that gives integers between 0 and M-1. I'm also asked to determine the period of this generator before it returns to the starting point/ seed value at x = 2.

Here's my code:

def lcr():
    M = 20
    c = 2 
    a = 30 
    x = 10 
    while x >= 10:
        x = ((a*x)+c) % M
        yield x

print(lcr)

When I print I get "<function lcr at 0x7f98d53cc790>" I'm not sure what I'm doing wrong.




Aucun commentaire:

Enregistrer un commentaire