Knuth shuffle: Difference between revisions

PascalABC.NET
(→‎{{header|RPL}}: improved code)
(PascalABC.NET)
 
(4 intermediate revisions by 2 users not shown)
Line 3,753:
-4 1 -1 -5 5 2 0 3 -2 -3 4
-3 -5 4 2 -4 0 5 3 1 -1 -2</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
procedure Shuffle<T>(a: array of T);
begin
for var i := a.Length - 1 downto 1 do
Swap(a[i], a[Random(i + 1)]);
end;
 
begin
var a := Arr(1..9);
Shuffle(a);
a.Print;
end.
</syntaxhighlight>
{{out}}
<pre>
5 8 6 1 3 4 2 9 7
</pre>
 
=={{header|Perl}}==
Line 4,527 ⟶ 4,546:
DUP SIZE 2 '''FOR''' j
j RAND * CEIL
GET LAST OVER j GET PUT j ROT PUT
-1 '''STEP'''
≫ '<span style="color:blue">KNUTH</span>' STO
Line 5,213 ⟶ 5,232:
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}}==
228

edits