Shorten shuffle and improve its randomness (previous one declusters poorly)

This commit is contained in:
Sasha 2018-02-01 18:34:24 +01:00
parent 77bc2faa4e
commit 51436f4eb2

View file

@ -62,13 +62,8 @@ shuffle l =
aux (length l) l aux (length l) l
where where
aux n [] = return [] aux n [] = return []
aux n [a] = return [a] aux n (h:t) = do
aux n l = do
cut <- randomRIO (0, n-1) cut <- randomRIO (0, n-1)
let (top, bottom) = splitAt cut l shuffled <- shuffle t
shuffledTop <- aux cut top let (top, bottom) = splitAt cut shuffled
shuffledBottom <- aux (n - cut) bottom return $ top ++ h : bottom
reorder <- randomRIO (False, True)
return $ if reorder
then shuffledTop ++ shuffledBottom
else shuffledBottom ++ shuffledTop