Knuth shuffle: Difference between revisions

add RPL
(add RPL)
Line 4,432:
[15 1 51 20 45 29 43 8 13 3 41 35 11 7 37 9 38 17 32 48 40 25 44 18 14 50 42 34 2 21 12 4 26 19 23 24 28 46 36 10 5 16 6 49 22 33 39 47 31 52 30 27]
</pre>
 
=={{header|RPL}}==
Index in RPL lists and arrays starts at 1.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
DUP SIZE 2 '''FOR''' j
j RAND * FLOOR 1 +
DUP2 GET 3 PICK j GET SWAP 4 ROLLD PUT j ROT PUT
-1 '''STEP'''
≫ ‘'''KNUTH'''’ STO
|
'''KNUTH''' ''( { list } -- { shuffled } )''
for j from last downto 2 do:
let k = random integer in range 0 ≤ j ≤ i
swap items[j] with items[k]
|}
 
=={{header|Ruby}}==
1,151

edits