RosettaCodeData/Task/Knuth-shuffle/F-Sharp/knuth-shuffle-2.fs
2023-07-01 13:44:08 -04:00

10 lines
622 B
FSharp

let KnuthShuffle (lst : array<'a>) = // '
let Swap i j = // Standard swap
let item = lst.[i]
lst.[i] <- lst.[j]
lst.[j] <- item
let rnd = new Random()
let ln = lst.Length
[0..(ln - 2)] // For all indices except the last
|> Seq.iter (fun i -> Swap i (rnd.Next(i, ln))) // swap th item at the index with a random one following it (or itself)
lst // Return the list shuffled in place