To use random number monad inside function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

To use random number monad inside function

Erick González
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090127/97ba2776/attachment.htm
Reply | Threaded
Open this post in threaded view
|

To use random number monad inside function

David Frey
On Tue, 27 Jan 2009 21:33:45 -0600, Erick Gonz?lez <[hidden email]>
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
Reply | Threaded
Open this post in threaded view
|

To use random number monad inside function

Krzysztof Skrzętnicki
In reply to this post by Erick González
You need to store random generator seed somewhere and keep track of updated
value after calling randomness-related functions.You can try doing it
manually, but what really fits here is monad that keeps track of that seed
value. Good news is: there is already package with such monad on Hackage. In
particular, try package MonadRandom (
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom-0.1.3 ).
If you have cabal-install, you can simply get it with "cabal install
monadrandom" (and if you don't have cabal-install, you should get this
wonderful tool right now!).

There are some examples within the documentation here:
http://hackage.haskell.org/packages/archive/MonadRandom/0.1.3/doc/html/Control-Monad-Random.html#1

All best

Christopher Skrz?tnicki

On Wed, Jan 28, 2009 at 04:33, Erick Gonz?lez <[hidden email]> 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
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090128/96e85197/attachment-0001.htm
Reply | Threaded
Open this post in threaded view
|

To use random number monad inside function

Thomas Davie
In reply to this post by Erick González

On 28 Jan 2009, at 04:33, Erick Gonz?lez 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?

There's a problem with doing this ? Haskell guarentees that no matter  
what (well, okay, not quite, but unsafePerformIO doesn't count), if  
you give the same arguments to a function, you'll get the same  
results.  This is known as referential transparency.  This is what  
gets us some of the biggest gains from the language (e.g. without it,  
we wouldn't be allowed to use our choice of non-strict evaluation  
order, we wouldn't be allowed to automatically parallelise, etc).

So, how is this problem solved?  In a couple of interesting ways.  
First is the way you've already seen, we can lock random number  
generation in a monad (usually the IO monad).  What this does, is it  
guarentees that the same IO action will always be returned by the  
function, but when that IO action gets shoved into the runtime, it can  
do any non-referentially transparent thing it likes, but that's  
"okay", because we're out of haskell land.

Understandably, this isn't the nicest way to do things, because it  
locks us in the IO monad, and in the evaluation of the action we lose  
all the advantages we had.  Not only that, but the programming style  
is fairly imperative.  So, we can think of a couple of ways round the  
restruction ? in order to give different outputs, we must have  
different inputs, so lets think about a couple of ways of doing that:

Firstly, we can pass a 'seed' into our pure function with which to  
generate further random numbers.  Secondly, we can use Haskell's  
ability to deal with infinitely large data structures, and pass in an  
infinitely long list of 'random' numbers.  Here's the second one:

main = do
   seed <- do something to generate a seed
   interact $ pureMain . randoms $ seed

pureMain :: [Int] -> String -> String
pureMain rs "jam" = show (take 20 rs)

Hope that helps

Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090128/96a913bb/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Re: To use random number monad inside function

Heinrich Apfelmus
In reply to this post by Erick González
Erick Gonz?lez 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

Section 3.1 of

    http://en.wikibooks.org/wiki/Haskell/Understanding_monads

explains how to work with random numbers in Haskell.


Regards,
apfelmus

--
http://apfelmus.nfshost.com