Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Generating Random Float (MAN)


----------------------------------------------------------------------

Message: 1
Date: Fri, 23 Apr 2010 12:23:14 -0300
From: MAN <[email protected]>
Subject: Re: [Haskell-beginners] Generating Random Float
To: Giorgio Stefanoni <[email protected]>
Cc: [email protected]
Message-ID: <1272036194.2345.1.ca...@dy-book>
Content-Type: text/plain; charset="UTF-8"

Works great here...

Using ghci:

Prelude> :m + Random
Prelude Random> let rand :: (Num a,Random a) => IO a; rand = newStdGen
>>= return . fst . randomR (0,1)
Prelude Random> rand :: IO Int
1
Prelude Random> rand :: IO Int
1
Prelude Random> rand :: IO Int
1
Prelude Random> rand :: IO Int
1
Prelude Random> rand :: IO Int
0
Prelude Random> rand :: IO Double
0.6897753065428174
Prelude Random> rand :: IO Float
0.6159074
Prelude Random> 

El vie, 23-04-2010 a las 15:33 +0200, Giorgio Stefanoni escribió:
> 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
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 22, Issue 36
*****************************************

Reply via email to