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
1 changed files with 5 additions and 10 deletions

View File

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