For fun I made this deck shuffling function to mimic how people imperfectly shuffle a deck. They cut it nearly in half and use the "ruffle" method to interweave the left and right decks together and then they repeat the process any number of times. The deck is never perfectly weaved together. You might shuffle them like L1, L2, R1, L3, R2, L4, R3, etc. or R1, L1, R2, R3, L2, etc. . The problem in my code is that successive reshuffles never switch the first and last cards of the deck. If in the first shuffle the bias says put L1 at the top of the deck, then every reshuffle will also have L1 at the top. I moved bias = random.random()
into the while loop so it should reset every time and therefore sometimes change the order. Is this some weird instance of pseudo random and recursive code not playing well together?
def shuffle_human(cards,reshuffle):
split = random.randint(-1*int(len(cards)/20),int(len(cards)/20)) #creates a bias to imperfectly split deck +- 5% of the deck size
L = cards[:int(len(cards)/2)+split] # creates left deck
R = cards[int(len(cards)/2)+split:] # creates right deck
D =[] # empty new deck
while len(D)< len(cards):
bias = random.random() # creates a bias to "incorrectly" choose
if L and bias <=.5: # which deck the next card will come from**strong text**
l = L.pop(0) # pops the card from the deck and appends it in.
print(l) # formatted this way so i can see whats going on
D.append(l)
if R and bias >.5: # same thing for right deck
r = R.pop(0)
print(r)
D.append(r)
print(D)
if reshuffle>0: # see if there are any reshuffles attempts needed
shuffle_perfect(D,reshuffle-1) # recursive call to reshuffle the deck.
shuffle_human(deck,3)
Problematic output
[0, 5, 6, 7, 8, 1, 9, 10, 2, 3, 4] # initial shuffle
[0, 1, 5, 9, 6, 10, 7, 2, 8, 3, 4] # reshuffle 1
[0, 10, 1, 7, 5, 2, 9, 8, 6, 3, 4] # 2
[0, 2, 10, 9, 1, 8, 7, 6, 5, 3, 4] # 3
As you can see it always has L1 and Ln-1 or R1 and Rn-1 as the first and last digits of the output deck, depending on the result of the first shuffle. No matter how many reshuffles I do. What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire