Knuth shuffle: Difference between revisions
Content added Content deleted
m (→{{header|BASIC}}: alphabetized subheaders) |
m (→{{header|Joy}}) |
||
Line 2,754: | Line 2,754: | ||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
< |
<syntaxhighlight lang=Joy>DEFINE knuth-shuffle == |
||
(* Take the size of the array (without destroying it) *) |
(* Take the size of the array (without destroying it) *) |
||
dup dup size |
dup dup size |
||
(* Generate a list of as many random numbers *) |
(* Generate a list of as many random numbers *) |
||
[rand] [rem] enconcat map |
[rand] [rem] enconcat map |
||
(* Zip the two lists *) |
(* Zip the two lists *) |
||
swap zip |
swap zip |
||
(* Sort according to the new index number *) |
(* Sort according to the new index number *) |
||
[small] [] [uncons unswonsd [first >] split [swons] dip2] |
[small] [] [uncons unswonsd [first >] split [swons] dip2] |
||
[enconcat] binrec |
[enconcat] binrec |
||
(* Delete the new index number *) |
(* Delete the new index number *) |
||
[second] map.</ |
[second] map.</syntaxhighlight> |
||
Using knuth-shuffle (file shuffle.joy): |
Using knuth-shuffle (file shuffle.joy): |
||
< |
<syntaxhighlight lang=Joy>(* Sorted array of 21 integers *) |
||
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] |
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] |
||
knuth-shuffle.</ |
knuth-shuffle.</syntaxhighlight> |
||
Command line: |
Command line: |
||
: <tt>joy shuffle.joy</tt> |
: <tt>joy shuffle.joy</tt> |