Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell QuickCheck Unique Random Number Generation

Does anyone know exactly how to define a generator in Haskell using QuickCheck such that chosen elements are picked only ONCE?

I've gotten as far as realizing that I might need a "Gen (Maybe Positive)" generator, but of course that will generate number with repetition. I want it so that the numbers chosen are picked without repetition. In the instance when a number is returned I want Just returned and in the instance where the randoms are all exhausted I want Gen Nothing returned.

Thanks,

Mark

like image 425
Mark Avatar asked Feb 24 '23 11:02

Mark


1 Answers

You can't. Look at the definition of Gen. There's no way for it to carry any state about what has been picked so far. Given the same random generator and size limit it must always generate the same result. You can however write a Eq a => Gen [a] that generates a list of values with no repetitions. A simple (but somewhat naive) one would be something like this.

uniques :: Eq a => Gen a -> Gen [a]
uniques gen = fmap nub $ listOf gen
like image 96
hammar Avatar answered Mar 05 '23 09:03

hammar