[Haskell-beginners] Generating Random Float
Giorgio Stefanoni
giorgio.stefanoni at gmail.com
Fri Apr 23 09:33:43 EDT 2010
Hi guys,
I'm working with Haskell for a little project at the university and I'm
stuck with a problem.
I'm trying to generate a random number in the interval [0.0,1.0]. It is
straightforward for me to implement the function on ints:
rand :: IO Int
rand = do r <- newStdGen
let (x1, r2) = randomR (0,1) r
print x1
return x1
However, if I try to generate a Float instead, I always get an Arithmetic
Overflow error that I can not understand.
Code:
rand :: IO Float
rand = do r <- newStdGen
let (x1, r2) = randomR (0.0::Float,1.0::Float) r
print x1
return x1
Do you have any clue why this happens?
Thanks in advance,
--
Giorgio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100423/9b397bea/attachment.html
More information about the Beginners
mailing list