Knuth shuffle: Difference between revisions

Content added Content deleted
(→‎{{header|RPL}}: formatting)
m (→‎{{header|RPL}}: typos, minor code optimization)
Line 4,435: Line 4,435:


=={{header|RPL}}==
=={{header|RPL}}==
The index of RPL lists and arrays starts at 1.
Indexes of RPL lists and arrays start at 1.
{{works with|Halcyon Calc|4.2.7}}
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
{| class="wikitable"
Line 4,444: Line 4,444:
DUP SIZE 2 '''FOR''' j
DUP SIZE 2 '''FOR''' j
j RAND * FLOOR 1 +
j RAND * CEIL
DUP2 GET 3 PICK j GET SWAP 4 ROLLD PUT j ROT PUT
DUP2 GET 3 PICK j GET SWAP 4 ROLLD PUT j ROT PUT
-1 '''STEP'''
-1 '''STEP'''
≫ '<span style="color:blue">KNUTH</span>' STO
≫ '<span style="color:blue">KNUTH</span>' STO
|
|
<span style="color:blue">KNUTH</span> ''( {list} ➝ {list} )'' <span style="color:grey">// works also with [arrays]</span>
<span style="color:blue">KNUTH</span> ''( {items} ➝ {items} )'' <span style="color:grey">// works also with [items]</span>
for j from last downto 2 do:
for j from last downto 2 do:
let k = random integer in range 1 ≤ ji
let k = random integer in range 1 ≤ kj
swap items[j] with items[k]
swap items[j] with items[k]