fork download
  1. import System.Random
  2.  
  3. removeAt :: [a] -> Int -> [a]
  4. removeAt xs k = take k xs ++ drop (k + 1) xs
  5.  
  6. rnd_select :: RandomGen g => [a] -> Int -> g -> ([a], g)
  7. rnd_select xs n g = rnd_select' xs [] n g
  8. where
  9. rnd_select' xs ys n g
  10. | n == 0 = (ys, g)
  11. | otherwise = rnd_select' (removeAt xs k) ((xs !! k) : ys) (n - 1) g'
  12. where (k, g') = randomR (0, length xs - 1) g
  13.  
  14. rnd_selectIO :: [a] -> Int -> IO [a]
  15. rnd_selectIO xs n = getStdRandom $ rnd_select xs n
  16.  
  17. main = rnd_selectIO [1..10] 5 >>= putStrLn . show
Success #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
[2,6,9,5,3]