[Haskell-beginners] To use random number monad inside function

David Frey dpfrey at shaw.ca
Tue Jan 27 22:50:52 EST 2009


On Tue, 27 Jan 2009 21:33:45 -0600, Erick González <erickgc at instec.cu>
wrote:
> Hi:
> Haskell' s way of random number generation is strange to me, but I know
> how to do that. I' d like to know how can I call random numbers generated
> on the screen inside a classic function. I mean, I need to know the way
of
> calling random numbers (one different every time that I run) inside a
> function. Please, Could anybody help me?
> Thanks 

I'm not sure exactly what you're trying to get at, but if you were hoping
to implement something like this:

multiplyByRandom :: Int -> Int
multiplyByRandom n = n * {- Get random number somehow -}


That is not how things are done.

Instead, you would do something like this:

import System.Random (randomIO)

multiplyByRandom :: Int -> IO Int
multiplyByRandom n = do
    rand <- randomIO 
    return (n * rand)


When you said "classic function", did you mean pure function?

http://en.wikipedia.org/wiki/Pure_function


More information about the Beginners mailing list