
On Wed, May 27, 2009 at 4:55 PM, Thomas Friedrich
Hi everyone,
Using random numbers in Haskell is not entirely trivial (at least, still not for me) and again I am bagging my head against the Gen-Monad. I'd like to write a kind of bootstrap function
sample :: Int -> [a] -> [a] sample n xs = ...
that samples uniformly n elements from the list xs. I am not sure how to go about this. Would you try something like
sample1 :: StdGen -> Int -> [a] -> [a]
and later use this in an IO Monad, something along the lines of: do {g <- mkStdGen; return $ sample g n xs}, or would you write it like
sample2 :: Int -> [a] -> Gen [a]
and then use a function from QuickCheck like `generate` to get your samples?
You see, I don't even know how to start thinking about the problem.
If anyone got an idea, I'd be pleased if you could help me.
Cheers, Thomas
In general, I don't think you'd use QuickCheck to generate random numbers. QuickCheck is, for the most part, solely for testing. I would write sample1 :: [a] -> StdGen -> a sample :: [a] -> StdGen -> [a] -- defined in terms of sample1 and use the take function to trim the list to the number of items you want. (Lazy evaluation guarantees that the rest won't get evaluated.) Use mkStdGen to create a StdGen to pass to it. Alex