Last active
February 1, 2017 18:11
-
-
Save Hugoberry/28737cc738aa54ea72f56a4a4c3061f1 to your computer and use it in GitHub Desktop.
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite set—in plain terms, the algorithm shuffles the set. The algorithm effectively puts all the elements into a hat; it continually determines the next element by randomly drawing an element from the hat until no elements remain. The algorithm produces an unbiase…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(n) => | |
let | |
swapAnyOrder = (_,from,to)=> if from = to then _ else | |
(if from < to then swap(_,from,to) else swap(_,to,from)), | |
swap = (_,from,to)=> List.Range(_,0,from) | |
&{_{to}} | |
&List.Range(_,from+1,to-from-1) | |
&{_{from}} | |
&List.Range(_,to+1) | |
in | |
List.Accumulate({0..(n-2)},{0..n},(_,iterator)=> | |
swapAnyOrder(List.Buffer(_), | |
Number.Round(Number.RandomBetween(iterator,n)), | |
iterator | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle