From 51436f4eb2bc25abd940a5f3e36e3fe37b5c3e52 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 1 Feb 2018 18:34:24 +0100 Subject: [PATCH] Shorten shuffle and improve its randomness (previous one declusters poorly) --- KoiKoi.hs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/KoiKoi.hs b/KoiKoi.hs index 00a393a..5972897 100644 --- a/KoiKoi.hs +++ b/KoiKoi.hs @@ -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