Acquiring a random set of a specific size (w/o dups) from a range of Ints

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

On Mon, Jun 13, 2011 at 4:56 PM, michael rice
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.

On Mon, 2011-06-13 at 16:56 -0700, michael rice wrote:
Is there an (existing) way to select 5 Ints randomly (no duplicates) from a population, say 1-20 (inclusive)?
Does anything from random-extras look like it'll work? http://hackage.haskell.org/packages/archive/random-extras/0.17/doc/html/Data... Shuffle [1..20], then take 5?

On Mon, Jun 13, 2011 at 8:56 PM, michael rice
Is there an (existing) way to select 5 Ints randomly (no duplicates) from a population, say 1-20 (inclusive)?
Yes, already implemented in the monte-carlo package as sampleSubset [1], sampleSubset :: MonadMC m => [a] -> Int -> m [a] Complete example code for your example: evalMC (sampleSubset [1..20] 5) (mt19937 0) Cheers! [1] http://hackage.haskell.org/packages/archive/monte-carlo/0.4.1/doc/html/Contr... -- Felipe.

I seem to still be missing some things. I found mt19937 in GSL.Random.Gen, but there are two evalMCs, one in Control.Monad.MC and another in Control.Monad.MC.GSL. Which?
Michael
---------
Registering monte-carlo-0.4.1...Installing library in /home/michael/.cabal/lib/monte-carlo-0.4.1/ghc-7.0.2Registering monte-carlo-0.4.1...[michael@sabal ~]$ ghciGHCi, version 7.0.2: http://www.haskell.org/ghc/ :? for helpLoading package ghc-prim ... linking ... done.Loading package integer-gmp ... linking ... done.Loading package base ... linking ... done.Prelude> :m + Control.Monad.MC.ClassPrelude Control.Monad.MC.Class> evalMC (sampleSubset [1..20] 5) (mt19937 0)
<interactive>:1:1: Not in scope: `evalMC'
<interactive>:1:34: Not in scope: `mt19937'Prelude Control.Monad.MC.Class>
--- On Mon, 6/13/11, Felipe Almeida Lessa
Is there an (existing) way to select 5 Ints randomly (no duplicates) from a population, say 1-20 (inclusive)?
Yes, already implemented in the monte-carlo package as sampleSubset [1], sampleSubset :: MonadMC m => [a] -> Int -> m [a] Complete example code for your example: evalMC (sampleSubset [1..20] 5) (mt19937 0) Cheers! [1] http://hackage.haskell.org/packages/archive/monte-carlo/0.4.1/doc/html/Contr... -- Felipe.

On Thu, Jun 16, 2011 at 3:04 PM, michael rice
I seem to still be missing some things. I found mt19937 in GSL.Random.Gen, but there are two evalMCs, one in Control.Monad.MC and another in Control.Monad.MC.GSL. Which?
Both are actually the same, because Control.Monad.MC reexports from Control.Monad.MC.GSL. =) GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> :m + Control.Monad.MC Prelude Control.Monad.MC> evalMC (sampleSubset [1..20] 5) (mt19937 0) Loading package mtl-1.1.1.1 ... linking ... done. Loading package primitive-0.3.1 ... linking ... done. Loading package vector-0.7.0.1 ... linking ... done. Loading package gsl-random-0.4.2 ... linking ... done. Loading package monte-carlo-0.4.1 ... linking ... done. [20,4,6,17,19] Prelude Control.Monad.MC> evalMC (sampleSubset [1..20] 5) (mt19937 42) [8,16,18,4,12] Prelude Control.Monad.MC> evalMC (sampleSubset [1..20] 5) (mt19937 2938420) [18,3,2,9,14] Prelude Control.Monad.MC> evalMC (sampleSubset [1..20] 5) (mt19937 0) [20,4,6,17,19] Cheers, -- Felipe.
participants (4)
-
Alexander Solla
-
Arlen Christian Mart Cuss
-
Felipe Almeida Lessa
-
michael rice