The function at this link and reproduced below uses a yield instead of just return
. Also there is a while True
loop for some reason.
def lcg(modulus, a, c, seed):
while True:
seed = (a * seed + c) % modulus
yield seed
I want to use the lcg()
and have coded it up without the while loop and using return instead of yield. My code works fine on the fake decks of only 10 cards I have tested. But I need to ultimately shuffle 52 cards each time using my shuffle() which is based on code here .
def shuffle(deck,seed):
global modulus
global constant
global mult
global last
n = len(deck)
last = seed
for i in range(n):
last = lcg(modulus, mult, constant , last)
j = last % n-i
temp = deck[i-1]
deck[i-1] = deck[j]
deck[j] = temp
return deck
My question is, how can I better use the yield
and while
in shuffle()
in my app? (Employing yield
may be overkill because my app only shuffles one deck at a time, not many.)
(Btw, I am aware that the lcg()
code has statistical problems but I have not found any way to use built-in random()
features at python that enable me to supply my user with the seeds
as they are produced here so they can replay or share a shuffled deck. If you have a suggestion for that I would love to hear it.)