Knuth shuffle: Difference between revisions

 
(8 intermediate revisions by 6 users not shown)
Line 1,747:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
proc shuffle . a[] .
for i = len a[] downto 2
r = randomrandint i
swap a[r] a[i]
.
Line 1,891:
var max := self.Length;
for(int i := 0,; i < max,; i += 1)
{
var j := randomGenerator.nextInt(i,max);
Line 1,904:
public program()
{
var a := Array.allocate:(MAX).populate::(i => i );
console.printLine(a.randomize())
Line 4,527:
DUP SIZE 2 '''FOR''' j
j RAND * CEIL
DUP2 GET 3LAST PICKOVER j GET SWAP 4 ROLLD PUT j ROT PUT
-1 '''STEP'''
≫ '<span style="color:blue">KNUTH</span>' STO
Line 4,738:
[8,9,7,3,4,5,1,2,6]
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program knuth_shuffle;
setrandom(0);
 
array := [1..10];
print("Before shuffling:", array);
shuffle(array);
print("After shuffling: ", array);
 
proc shuffle(rw tup);
loop for i in [1..#tup-1] do
j := random [i+1..#tup];
[tup(i), tup(j)] := [tup(j), tup(i)];
end loop;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>Before shuffling: [1 2 3 4 5 6 7 8 9 10]
After shuffling: [7 8 1 10 2 5 6 9 4 3]</pre>
 
=={{header|Sidef}}==
Line 5,193 ⟶ 5,213:
after:
19 4 49 9 27 35 50 11 2 29 22 48 33 15 17 42 47 28 41 18 34 21 30 39 3 8 23 12 36 26 0 46 7 44 13 14 16 40 10 25 31 32 51 24 20 38 45 6 43 1 5 37</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
Build pairs of indexes to be swapped then apply these as a fold.
<syntaxhighlight lang="Uiua">
Knuth ← ∧(⍜⊏⇌)≡(⊟⌊×⚂.)⇌↘1⇡⧻.
Knuth ⇡10
</syntaxhighlight>
Typical output:
<pre>
[3 0 6 5 7 8 4 1 9 2]
</pre>
 
=={{header|UNIX Shell}}==
Line 5,423 ⟶ 5,455:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var rand = Random.new()
60

edits