module Data.Random.List where
import Data.Random.RVar
import Data.Random.Distribution.Uniform
import qualified System.Random.Shuffle as SRS
import Control.Monad
randomElement :: [a] -> RVar a
randomElement [] = error "randomElement: empty list!"
randomElement xs = do
n <- uniform 0 (length xs 1)
return (xs !! n)
shuffle :: [a] -> RVar [a]
shuffle [] = return []
shuffle xs = do
is <- zipWithM (\_ i -> uniform 0 i) (tail xs) [1..]
return (SRS.shuffle xs (reverse is))
shuffleN :: Int -> [a] -> RVar [a]
shuffleN n xs = shuffleNofM n n xs
shuffleNofM :: Int -> Int -> [a] -> RVar [a]
shuffleNofM 0 _ _ = return []
shuffleNofM n m xs
| n > m = error "shuffleNofM: n > m"
| n >= 0 = do
is <- sequence [uniform 0 i | i <- take n [m1, m2 ..1]]
return (take n $ SRS.shuffle (take m xs) is)
shuffleNofM _ _ _ = error "shuffleNofM: negative length specified"