mardi 31 juillet 2018

PRNG namespace Python

x = 0
a = 81
c = 337
m = 1000
def rand():
    global x
    x = (a * x + c) % m
    return x
def reset(mult, inc, mod):
    global x, a, c, m
    x = 0
    a = mult
    c = inc
    m = mod 

def roll(pup):
    newl = []
    for i in range(10):
        newl.append(rand() % pup + 1 )
    return newl

print(roll(6))

An outline of this code was given to me in my textbook but I do not understand how the rand function gives me a sequence of random numbers. Specifically, the first 3 numbers are

[2, 5, 2]

however, when I manually followed the code with pen and paper, every time there is a call to rand(), it should always be giving me 2 since

x = (a * x + c) % 6 + 1

which is essentially

x = (0 + 337) % 6 + 1

I even tried keeping the value of x to the last appended value of roll() so for instance, after the first iteration, rand() gives two, and so the next call to rand() is

x = (81 * 2 + 337) % 6 + 1

which still gives 2. What am I missing?




Aucun commentaire:

Enregistrer un commentaire