[Haskell-cafe] Generating random tuples

Jacek Generowicz jacek.generowicz at cern.ch
Mon Nov 1 14:18:33 EDT 2010


I'm toying with generating random objects (for example tuples) and  
started wondering what pearls of wisdom Cafe might have on the matter.  
Two obvious points (relating to my toy code, shown below) are

1) The meaning of the limits required by randomR is not obvious for  
types such as tuples (you could come up with some definition, but it  
wouldn't be unique: how would you allow for different ones?[*]; you  
might decide that having such limits is nonsensical and not want to  
provide a randomR: would you then leave it undefinded?).

[*] I guess this is related to issues such as Num being both a sum and  
and product monoid.

2) I'm sure there are at least half a dozen prettier definitions of  
random.

But I suspect that the juicy bits will be in offerings about issues  
that I haven't even dreamed about.

Presumably QuickCheck's test-data generation mechanism would be  
interesting to look at in this context. Is there a gentle explanation  
of how it works somewhere?



Here's my initial effort:

import Control.Monad
import System.Random

main :: IO (Int, Int)
main = randomIO

instance (Random a, Random b) => Random (a, b) where
     randomR = undefined
     random g0 = let (i1,g1) = random g0
                     (i2,g2) = random g1
                 in ((i1,i2), g1)



More information about the Haskell-Cafe mailing list