[Haskell-cafe] A Random Question

Dominic Steinitz dominic.steinitz at blueyonder.co.uk
Sat Dec 15 06:17:38 EST 2007

Benja Fallenstein wrote:
> Hi Dominic,
> myRandomR :: (Foo, Foo) -> MyGen -> (Foo, MyGen)
> but you need
> randomR :: RandomGen g => (Foo, Foo) -> g -> (Foo, g)
> i.e., the function is required to be more generic than the one you provide.

Thank you - obvious in hindsight.

> The good news is that you don't need to declare Foo at all. You only
> need to declare an instance of Random if you want to generate random
> *values* of some new type. If you just want numbers, remove all the
> stuff about Foo and call (for example)
> randomR (0,5) $ MyGen 18
> and it'll give you a number and a new MyGen.

But don't I need something in class Random so that I can use choose?

> choose :: forall a. (Random a) => (a, a) -> Gen a

Actually, looking in QuickCheck, I can see this approach is not going to
work as QuickCheck always picks the StdGen instance of RandomGen :-(

> choose :: Random a => (a, a) -> Gen a
> choose bounds = (fst . randomR bounds) `fmap` rand

> rand :: Gen StdGen
> rand = Gen (\n r -> r)

Does anyone have any ideas on a way to force QuickCheck to use a
different generator of random values?

Thanks, Dominic.

More information about the Haskell-Cafe mailing list