Card shuffles: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 44: | Line 44: | ||
If we generate a deck by |
If we generate a deck by |
||
< |
<syntaxhighlight lang=APL> |
||
deck ← ⊂[1](52⍴'A23456789TJQK'),[0.5](13⍴'S'),(13⍴'H'),(13⍴'D'),(13⍴'C') |
deck ← ⊂[1](52⍴'A23456789TJQK'),[0.5](13⍴'S'),(13⍴'H'),(13⍴'D'),(13⍴'C') |
||
</syntaxhighlight> |
|||
</lang> |
|||
Then a generated deck looks like |
Then a generated deck looks like |
||
< |
<syntaxhighlight lang=APL> |
||
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC |
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sorting a deck merely requires generating 52 unique random nunmbers from 1 to 52. |
Sorting a deck merely requires generating 52 unique random nunmbers from 1 to 52. |
||
<lang> |
<syntaxhighlight lang=text> |
||
deck[52?52] |
deck[52?52] |
||
JD 8C TH 8D KH QH 6S AH 4D JS 5S AD 6H 3H 3D 5C 9C 7C 7S 4C JC 3S KD 9H 3C 4H 2D TD KS TS 7D JH 9D 8H 6D 7H 2H 4S QC AC KC 9S AS QS TC 2C 8S 5D 2S 6C 5H QD |
JD 8C TH 8D KH QH 6S AH 4D JS 5S AD 6H 3H 3D 5C 9C 7C 7S 4C JC 3S KD 9H 3C 4H 2D TD KS TS 7D JH 9D 8H 6D 7H 2H 4S QC AC KC 9S AS QS TC 2C 8S 5D 2S 6C 5H QD |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Modula-2}} |
{{trans|Modula-2}} |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 234: | Line 234: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Riffle shuffle |
<pre>Riffle shuffle |
||
Line 254: | Line 254: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 370: | Line 370: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
||
Line 385: | Line 385: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang=cpp> |
||
#include <time.h> |
#include <time.h> |
||
#include <algorithm> |
#include <algorithm> |
||
Line 517: | Line 517: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 532: | Line 532: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=D>import std.container.array; |
||
import std.random; |
import std.random; |
||
import std.range; |
import std.range; |
||
Line 634: | Line 634: | ||
list.randomShuffle(); |
list.randomShuffle(); |
||
writeln(list); |
writeln(list); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
||
Line 653: | Line 653: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 750: | Line 750: | ||
}) |
}) |
||
fmt.Println(deck) |
fmt.Println(deck) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 770: | Line 770: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=groovy>class CardShuffles { |
||
private static final Random rand = new Random() |
private static final Random rand = new Random() |
||
Line 872: | Line 872: | ||
println(list) |
println(list) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
||
Line 892: | Line 892: | ||
{{eff note|J|({~ ?~@#)}} |
{{eff note|J|({~ ?~@#)}} |
||
< |
<syntaxhighlight lang=J>NB. overhand cut |
||
overhand=: (\: [: +/\ %@%:@# > # ?@# 0:)@]^:[ |
overhand=: (\: [: +/\ %@%:@# > # ?@# 0:)@]^:[ |
||
NB. Gilbert–Shannon–Reeds model |
NB. Gilbert–Shannon–Reeds model |
||
riffle=: (({.~+/)`(I.@])`(-.@]#inv (}.~+/))} ?@(#&2)@#)@]^:[</ |
riffle=: (({.~+/)`(I.@])`(-.@]#inv (}.~+/))} ?@(#&2)@#)@]^:[</syntaxhighlight> |
||
The probability of a cut occurring between each pair of cards in this overhand shuffle is proportional to the reciprocal of the square root of the number of cards in the deck. |
The probability of a cut occurring between each pair of cards in this overhand shuffle is proportional to the reciprocal of the square root of the number of cards in the deck. |
||
Line 904: | Line 904: | ||
Here are some examples of the underlying selection mechanism in action for a deck of 10 cards: |
Here are some examples of the underlying selection mechanism in action for a deck of 10 cards: |
||
< |
<syntaxhighlight lang=J> ([: +/\ %@%:@# > # ?@# 0:) i.10 |
||
0 0 0 0 0 0 0 0 1 1 |
0 0 0 0 0 0 0 0 1 1 |
||
([: +/\ %@%:@# > # ?@# 0:) i.10 |
([: +/\ %@%:@# > # ?@# 0:) i.10 |
||
Line 911: | Line 911: | ||
0 1 1 2 3 3 3 3 4 5 |
0 1 1 2 3 3 3 3 4 5 |
||
([: +/\ %@%:@# > # ?@# 0:) i.10 |
([: +/\ %@%:@# > # ?@# 0:) i.10 |
||
0 1 1 1 1 2 2 3 3 3</ |
0 1 1 1 1 2 2 3 3 3</syntaxhighlight> |
||
The final step of a cut is to sort the deck in descending order based on the numbers we compute this way. |
The final step of a cut is to sort the deck in descending order based on the numbers we compute this way. |
||
Line 919: | Line 919: | ||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang=J> 1 riffle i.20 |
||
0 1 2 3 4 5 6 7 8 13 14 9 15 16 17 10 18 11 12 19 |
0 1 2 3 4 5 6 7 8 13 14 9 15 16 17 10 18 11 12 19 |
||
10 riffle i.20 |
10 riffle i.20 |
||
Line 926: | Line 926: | ||
17 18 19 13 14 15 16 4 5 6 7 8 9 10 11 12 0 1 2 3 |
17 18 19 13 14 15 16 4 5 6 7 8 9 10 11 12 0 1 2 3 |
||
10 overhand i.20 |
10 overhand i.20 |
||
15 11 2 4 5 12 16 10 17 19 9 8 6 13 3 18 7 1 0 14</ |
15 11 2 4 5 12 16 10 17 19 9 8 6 13 3 18 7 1 0 14</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang=java5>import java.util.Arrays; |
||
import java.util.Collections; |
import java.util.Collections; |
||
import java.util.LinkedList; |
import java.util.LinkedList; |
||
Line 1,036: | Line 1,036: | ||
System.out.println(list + "\n"); |
System.out.println(list + "\n"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
||
Line 1,056: | Line 1,056: | ||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang=julia>function riffleshuffle!(list::Vector, flips::Integer) |
||
len = length(list) |
len = length(list) |
||
# pre-allocate the left and right part for efficiency |
# pre-allocate the left and right part for efficiency |
||
Line 1,121: | Line 1,121: | ||
v = collect(1:20) |
v = collect(1:20) |
||
println("# Default shuffle:\n", v) |
println("# Default shuffle:\n", v) |
||
println(" -> ", shuffle!(v), "\n")</ |
println(" -> ", shuffle!(v), "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,145: | Line 1,145: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang=scala>// version 1.1.51 |
||
import java.util.Random |
import java.util.Random |
||
Line 1,211: | Line 1,211: | ||
shuffle(deck) // shuffles deck in place |
shuffle(deck) // shuffles deck in place |
||
println(deck) |
println(deck) |
||
}</ |
}</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
Line 1,229: | Line 1,229: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang=lua>-- Return a table respresenting a standard deck of cards in order |
||
function newDeck () |
function newDeck () |
||
local cards, suits = {}, {"C", "D", "H", "S"} |
local cards, suits = {}, {"C", "D", "H", "S"} |
||
Line 1,293: | Line 1,293: | ||
deck2 = overhand(deck2) |
deck2 = overhand(deck2) |
||
print("Sorted deck after one overhand shuffle:") |
print("Sorted deck after one overhand shuffle:") |
||
show(deck2)</ |
show(deck2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Sorted deck after one riffle shuffle: |
<pre>Sorted deck after one riffle shuffle: |
||
Line 1,305: | Line 1,305: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang=modula2>MODULE CardShuffles; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM RandomNumbers IMPORT Random; |
FROM RandomNumbers IMPORT Random; |
||
Line 1,502: | Line 1,502: | ||
ReadChar; |
ReadChar; |
||
END CardShuffles.</ |
END CardShuffles.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang=Nim>import algorithm, deques, random, sequtils, strutils |
||
proc riffle(deck: seq[int]; iterations: Positive): seq[int] = |
proc riffle(deck: seq[int]; iterations: Positive): seq[int] = |
||
Line 1,557: | Line 1,557: | ||
echo "\nStandard library shuffle with one iteration:" |
echo "\nStandard library shuffle with one iteration:" |
||
deck.shuffle() # Shuffles deck in place. |
deck.shuffle() # Shuffles deck in place. |
||
echo deck.join(" ")</ |
echo deck.join(" ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,574: | Line 1,574: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Riffle shuffle: |
Riffle shuffle: |
||
< |
<syntaxhighlight lang=parigp>riffle(v)= |
||
{ |
{ |
||
my(n=#v,k,t,deck=vector(n),left,right); |
my(n=#v,k,t,deck=vector(n),left,right); |
||
Line 1,591: | Line 1,591: | ||
vecextract(v, deck); |
vecextract(v, deck); |
||
} |
} |
||
addhelp(riffle, "riffle(v): Riffle shuffles the vector v, following the Gilbert-Shannon-Reeds model.");</ |
addhelp(riffle, "riffle(v): Riffle shuffles the vector v, following the Gilbert-Shannon-Reeds model.");</syntaxhighlight> |
||
Overhand shuffle: |
Overhand shuffle: |
||
< |
<syntaxhighlight lang=parigp>overhand(v)= |
||
{ |
{ |
||
my(u=[],t,n=2*#v\5); |
my(u=[],t,n=2*#v\5); |
||
Line 1,604: | Line 1,604: | ||
u; |
u; |
||
} |
} |
||
addhelp(overhand, "overhand(v): Overhand shuffles the vector v.");</ |
addhelp(overhand, "overhand(v): Overhand shuffles the vector v.");</syntaxhighlight> |
||
Usage: |
Usage: |
||
< |
<syntaxhighlight lang=parigp>riffle([1..52]) |
||
overhand([1..52])</ |
overhand([1..52])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = [1, 2, 3, 21, 4, 22, 23, 5, 24, 25, 26, 6, 27, 28, 29, 30, 7, 31, 32, 33, 34, 35, 36, 8, 37, 38, 39, 40, 9, 10, 11, 12, 41, 42, 43, 13, 44, 45, 14, 46, 47, 48, 15, 16, 17, 49, 50, 18, 51, 19, 20, 52] |
<pre>%1 = [1, 2, 3, 21, 4, 22, 23, 5, 24, 25, 26, 6, 27, 28, 29, 30, 7, 31, 32, 33, 34, 35, 36, 8, 37, 38, 39, 40, 9, 10, 11, 12, 41, 42, 43, 13, 44, 45, 14, 46, 47, 48, 15, 16, 17, 49, 50, 18, 51, 19, 20, 52] |
||
Line 1,615: | Line 1,615: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Follows the Raku implementation for the overhand shuffle, but uses classic one-liner for riffle. |
Follows the Raku implementation for the overhand shuffle, but uses classic one-liner for riffle. |
||
< |
<syntaxhighlight lang=perl>sub overhand { |
||
our @cards; local *cards = shift; |
our @cards; local *cards = shift; |
||
my(@splits,@shuffle); |
my(@splits,@shuffle); |
||
Line 1,639: | Line 1,639: | ||
@cards = 1..20; |
@cards = 1..20; |
||
riffle(\@cards) for 1..10; |
riffle(\@cards) for 1..10; |
||
print join ' ', @cards, "\n";</ |
print join ' ', @cards, "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9 11 5 2 4 14 1 3 8 6 15 13 16 12 19 20 7 18 10 17 |
<pre>9 11 5 2 4 14 1 3 8 6 15 13 16 12 19 20 7 18 10 17 |
||
Line 1,645: | Line 1,645: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang=Phix>(phixonline)--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">riffle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">riffle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
Line 1,690: | Line 1,690: | ||
<span style="color: #000000;">show_deck</span><span style="color: #0000FF;">(</span><span style="color: #000000;">overhand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">DECKSIZE</span><span style="color: #0000FF;">)))</span> |
<span style="color: #000000;">show_deck</span><span style="color: #0000FF;">(</span><span style="color: #000000;">overhand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">DECKSIZE</span><span style="color: #0000FF;">)))</span> |
||
<span style="color: #000000;">show_deck</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">DECKSIZE</span><span style="color: #0000FF;">)))</span> |
<span style="color: #000000;">show_deck</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">DECKSIZE</span><span style="color: #0000FF;">)))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,699: | Line 1,699: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>(load "@lib/simul.l") |
||
(de riffle (Lst) |
(de riffle (Lst) |
||
Line 1,715: | Line 1,715: | ||
(println 'riffle (riffle (range 1 19)) ) |
(println 'riffle (riffle (range 1 19)) ) |
||
(println 'overhand (overhand (range 1 19)) ) |
(println 'overhand (overhand (range 1 19)) ) |
||
(println 'shuffle (shuffle (range 1 19)) )</ |
(println 'shuffle (shuffle (range 1 19)) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,725: | Line 1,725: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang=python>import random |
||
def riffleShuffle(va, flips): |
def riffleShuffle(va, flips): |
||
Line 1,806: | Line 1,806: | ||
random.shuffle(nums) |
random.shuffle(nums) |
||
print nums |
print nums |
||
print</ |
print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Riffle shuffle |
<pre>Riffle shuffle |
||
Line 1,836: | Line 1,836: | ||
Racket has a built in <code>shuffle</code> function. Frankly, I'd go with that in your own code! |
Racket has a built in <code>shuffle</code> function. Frankly, I'd go with that in your own code! |
||
< |
<syntaxhighlight lang=racket>#lang typed/racket |
||
;; --------------------------------------------------------------------------------------------------- |
;; --------------------------------------------------------------------------------------------------- |
||
;; Types and shuffle builder |
;; Types and shuffle builder |
||
Line 1,977: | Line 1,977: | ||
(printf "wash piles: ~.a~%" (wash-pile-shuffle unshuffled-pack 1)) |
(printf "wash piles: ~.a~%" (wash-pile-shuffle unshuffled-pack 1)) |
||
;; Or there is always the built-in shuffle: |
;; Or there is always the built-in shuffle: |
||
(printf "shuffle: ~.a~%" (shuffle unshuffled-pack)))</ |
(printf "shuffle: ~.a~%" (shuffle unshuffled-pack)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,000: | Line 2,000: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang=raku line> |
||
use v6; |
use v6; |
||
Line 2,026: | Line 2,026: | ||
say (^20).pick(*); |
say (^20).pick(*); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,032: | Line 2,032: | ||
Six-handed 500 has additional cards of: <big> ♣11 ♣12 ♠11 ♠12 ♦11 ♦12 ♦13 ♦11 ♦12 ♦13 </big> |
Six-handed 500 has additional cards of: <big> ♣11 ♣12 ♠11 ♠12 ♦11 ♦12 ♦13 ♦11 ♦12 ♦13 </big> |
||
< |
<syntaxhighlight lang=rexx>/*REXX program simulates various types of shuffling a deck of cards (any kind of deck).*/ |
||
call create; call show 'new deck' /*build and display a new card deck. */ |
call create; call show 'new deck' /*build and display a new card deck. */ |
||
Line 2,086: | Line 2,086: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: _=@.1; do j=2 for #-1; _=_ @.j; end /*j*/; L = length(_) |
show: _=@.1; do j=2 for #-1; _=_ @.j; end /*j*/; L = length(_) |
||
say center( arg(1), L, '═'); say _; say; return /*show deck*/</ |
say center( arg(1), L, '═'); say _; say; return /*show deck*/</syntaxhighlight> |
||
{{out|output}} |
{{out|output}} |
||
<pre style="font-size:125%"> |
<pre style="font-size:125%"> |
||
Line 2,106: | Line 2,106: | ||
Two methods to solve the requirements, and a third one as bonus. |
Two methods to solve the requirements, and a third one as bonus. |
||
< |
<syntaxhighlight lang=Ruby> |
||
def riffle deck |
def riffle deck |
||
left, right = deck.partition{rand(10).odd?} |
left, right = deck.partition{rand(10).odd?} |
||
Line 2,136: | Line 2,136: | ||
p overhand(deck) |
p overhand(deck) |
||
p bonus(deck) |
p bonus(deck) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=scala>import scala.collection.mutable.ListBuffer |
||
import scala.util.Random |
import scala.util.Random |
||
Line 2,243: | Line 2,243: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) |
<pre>List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) |
||
Line 2,261: | Line 2,261: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang=Tcl> |
||
proc riffle deck { |
proc riffle deck { |
||
set length [llength $deck] |
set length [llength $deck] |
||
Line 2,274: | Line 2,274: | ||
puts [riffle [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]] |
puts [riffle [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]] |
||
puts [overhand [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]] |
puts [overhand [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang=vbnet>Imports System.Runtime.CompilerServices |
||
Imports System.Text |
Imports System.Text |
||
Line 2,394: | Line 2,394: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
||
Line 2,410: | Line 2,410: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang=ecmascript>import "random" for Random |
||
var r = Random.new() |
var r = Random.new() |
||
Line 2,472: | Line 2,472: | ||
System.print("\nStandard library shuffle with 1 iteration:") |
System.print("\nStandard library shuffle with 1 iteration:") |
||
r.shuffle(deck) // shuffles deck in place |
r.shuffle(deck) // shuffles deck in place |
||
System.print(deck)</ |
System.print(deck)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,492: | Line 2,492: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
A much better shuffle is List's shuffle method. |
A much better shuffle is List's shuffle method. |
||
< |
<syntaxhighlight lang=zkl>fcn riffle(deck){ |
||
len,N:=deck.len(),len/2; |
len,N:=deck.len(),len/2; |
||
newDeck:=N.pump(List,'wrap(n){ return(Void.Write,deck[n],deck[N+n]) }); |
newDeck:=N.pump(List,'wrap(n){ return(Void.Write,deck[n],deck[N+n]) }); |
||
Line 2,501: | Line 2,501: | ||
len,N,piles:=deck.len(),(0.2*len).toInt(),(len.toFloat()/N).ceil().toInt(); |
len,N,piles:=deck.len(),(0.2*len).toInt(),(len.toFloat()/N).ceil().toInt(); |
||
piles.pump(List,'wrap(n){ deck[n*N,N] }).reverse().flatten() |
piles.pump(List,'wrap(n){ deck[n*N,N] }).reverse().flatten() |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang=zkl>riffle( [1..19].walk()).println(); |
||
overHand([1..19].walk()).println(); |
overHand([1..19].walk()).println(); |
||
[1..19].walk().shuffle().println();</ |
[1..19].walk().shuffle().println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |