[Haskell-cafe] Acquiring a random set of a specific size (w/o dups) from a range of Ints

Alexander Solla alex.solla at gmail.com
Tue Jun 14 02:27:50 CEST 2011


On Mon, Jun 13, 2011 at 4:56 PM, michael rice <nowgate at yahoo.com> wrote:

> Is there an (existing) way to select 5 Ints randomly (no duplicates) from a
> population, say 1-20 (inclusive)?
>
> Michael
>

This is as close as I have gotten, but it is only probabilistically true.

take_n_unique_randoms_in_range :: ( Ord random
                                  , Random random
                                  , MonadIO io
                                  ) => Int -> (random, random) -> io
[random]
take_n_unique_randoms_in_range n (a,b) = liftM ((take n) . nub) .
(replicateM (3*n)) . liftIO $ randomRIO (a,b)

I'm going to put making an iteratee-based solution on my agenda.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110613/6f0b5c3b/attachment.htm>


More information about the Haskell-Cafe mailing list