/ Published in: Haskell
URL: http://haskell.org/haskellwiki/99_questions/21_to_28
problem 23, Extract a given number of randomly selected elements from a list. Example:
Prelude System.Random>rnd_select "abcdefgh" 3 >>= putStrLn
"eda"
Two problems: 1) How to return a list, and 2) how to sample without duplication
Expand |
Embed | Plain Text
-- ah, randomization. Always fun in Haskell. rnd_select_one xs = do n' <- randomRIO (0, len) -- this version allows resampling. -- Ideally, to prevent re-sampling, we would use partition from Data.List -- like so: -- partition (`elem` "g") "abcdefghijklmn" -- --> ("g", "abcdefhijklmn") -- I'll have to revisit this one when I have a chance to -- play around with partitioning.
You need to login to post a comment.
