#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    [Haskell] Generating a random Float number


    Hi guys,

    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:

    Code:
    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Works for me using GHC 6.8.3.

    Can you provide a complete compilable program (or an interactive session) that shows the problem?

    What compiler/interpreter are you using?
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo