I was wondering if it's possible to generate random tuples with a code like:
take 4 $ randomRs ((0,0),(70,100)) $ mkStdGen x :: [(Double,Double)]
when I try this one I get the error:
No instance for (Random (Float, Float)) arising from a use of 'randoms'
Is there a way to get random tuples without using zip
?
Basically the error message is saying that there isn't a defined way of making random tuples. But you can of course add one yourself.
Off the top of my head (i.e., I haven't actually tested this), you can do something like
instance (Random x, Random y) => Random (x, y) where
randomR ((x1, y1), (x2, y2)) gen1 =
let (x, gen2) = randomR (x1, x2) gen1
(y, gen3) = randomR (y1, y2) gen2
in ((x, y), gen3)
Now you can use randomR
on tuples (provided that the types in the tuple support random generation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With