Card shuffles: Difference between revisions
Content added Content deleted
(Modified task description) |
|||
Line 1,142: | Line 1,142: | ||
shuffle (5 3 13 15 17 12 14 11 2 1 19 7 6 9 18 8 10 4 16) |
shuffle (5 3 13 15 17 12 14 11 2 1 19 7 6 9 18 8 10 4 16) |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
{{trans|D}} |
|||
<lang python>import random |
|||
def riffleShuffle(va, flips): |
|||
nl = va |
|||
for n in range(flips): |
|||
#cut the deck at the middle +/- 10%, remove the second line of the formula for perfect cutting |
|||
cutPoint = len(nl)/2 + random.choice([-1, 1]) * random.randint(0, len(va)/10) |
|||
# split the deck |
|||
left = nl[0:cutPoint] |
|||
right = nl[cutPoint:] |
|||
del nl[:] |
|||
while (len(left) > 0 and len(right) > 0): |
|||
#allow for imperfect riffling so that more than one card can come form the same side in a row |
|||
#biased towards the side with more cards |
|||
#remove the if and else and brackets for perfect riffling |
|||
if (random.uniform(0, 1) >= len(left) / len(right) / 2): |
|||
nl.append(right.pop(0)) |
|||
else: |
|||
nl.append(left.pop(0)) |
|||
if (len(left) > 0): |
|||
nl = nl + left |
|||
if (len(right) > 0): |
|||
nl = nl + right |
|||
return nl |
|||
def overhandShuffle(va, passes): |
|||
mainHand = va |
|||
for n in range(passes): |
|||
otherHand = [] |
|||
while (len(mainHand) > 0): |
|||
#cut at up to 20% of the way through the deck |
|||
cutSize = random.randint(0, len(va) / 5) + 1 |
|||
temp = [] |
|||
#grab the next cut up to the end of the cards left in the main hand |
|||
i=0 |
|||
while (i<cutSize and len(mainHand) > 0): |
|||
temp.append(mainHand.pop(0)) |
|||
i = i + 1 |
|||
#add them to the cards in the other hand, sometimes to the front sometimes to the back |
|||
if (random.uniform(0, 1) >= 0.1): |
|||
#front most of the time |
|||
otherHand = temp + otherHand |
|||
else: |
|||
otherHand = otherHand + temp |
|||
#move the cards back to the main hand |
|||
mainHand = otherHand |
|||
return mainHand |
|||
print "Riffle shuffle" |
|||
nums = [x+1 for x in range(21)] |
|||
print nums |
|||
print riffleShuffle(nums, 10) |
|||
print |
|||
print "Riffle shuffle" |
|||
nums = [x+1 for x in range(21)] |
|||
print nums |
|||
print riffleShuffle(nums, 1) |
|||
print |
|||
print "Overhand shuffle" |
|||
nums = [x+1 for x in range(21)] |
|||
print nums |
|||
print overhandShuffle(nums, 10) |
|||
print |
|||
print "Overhand shuffle" |
|||
nums = [x+1 for x in range(21)] |
|||
print nums |
|||
print overhandShuffle(nums, 1) |
|||
print |
|||
print "Library shuffle" |
|||
nums = [x+1 for x in range(21)] |
|||
print nums |
|||
random.shuffle(nums) |
|||
print nums |
|||
print</lang> |
|||
{{out}} |
|||
<pre>Riffle shuffle |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] |
|||
[4, 16, 5, 19, 3, 14, 2, 9, 20, 13, 17, 10, 6, 7, 1, 18, 12, 11, 8, 21, 15] |
|||
Riffle shuffle |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] |
|||
[13, 14, 15, 1, 16, 2, 3, 17, 4, 5, 18, 6, 7, 19, 8, 9, 20, 10, 11, 21, 12] |
|||
Overhand shuffle |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] |
|||
[21, 12, 5, 16, 7, 2, 15, 14, 20, 6, 8, 11, 13, 1, 4, 17, 19, 9, 3, 18, 10] |
|||
Overhand shuffle |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] |
|||
[21, 20, 19, 18, 16, 17, 14, 15, 11, 12, 13, 4, 5, 6, 7, 1, 2, 3, 8, 9, 10] |
|||
Library shuffle |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] |
|||
[14, 12, 2, 17, 18, 21, 8, 4, 15, 9, 11, 10, 3, 1, 7, 19, 20, 6, 5, 16, 13]</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 1,311: | Line 1,416: | ||
shuffle: (J♣ 2♠ 4♦ A♦ K♥ 6♦ 5♦ 8♣ 2♦ T♥ 4♠ 3♣ 7♦ 9♠ T♦ J... |
shuffle: (J♣ 2♠ 4♦ A♦ K♥ 6♦ 5♦ 8♣ 2♦ T♥ 4♠ 3♣ 7♦ 9♠ T♦ J... |
||
</pre> |
</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |