Card shuffles: Difference between revisions

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

edits