
Hello, I have an application for random numbers. So far, in my journey with Haskell, I have learned basics and a few things about monads, and I was hoping I could get some guidance how to employ random # gens. I need to run some simulated experiments to calculate some statistics by a Monte Carlo method. There are several components to the experiment, but one component is: - Given a hat with 5 red squares and 5 blue squares, draw one square at a time, note its color, for a total of 8 squares. Remember the order of the 8 squares. Another function will take two inputs each of which is "red" or "blue". If they are the same color, return the other color. Otherwise "toss a coin" to determine which color to return. -- this uses Bool rather than "red" "blue" for obvious reasons. balanceColors :: Bool -> Bool -> Bool balanceColors c1 c2 = if c1 == c2 then not c1 else -- flip coin -- Can I get a few examples or pointers? I believe I will have to run this in a State monad or the IO monad, will I not? Thanks, Mike

On Jul 24, 2009, at 01:35 , Michael P Mossey wrote:
Can I get a few examples or pointers? I believe I will have to run this in a State monad or the IO monad, will I not?
IO is only needed to get an initial seed. The standard random number functions are designed for use with State, but you might also want to look at http://hackage.haskell.org/package/MonadRandom . -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Thu, 23 Jul 2009, Michael P Mossey wrote:
Hello,
I have an application for random numbers. So far, in my journey with Haskell, I have learned basics and a few things about monads, and I was hoping I could get some guidance how to employ random # gens.
I need to run some simulated experiments to calculate some statistics by a Monte Carlo method.
Can I get a few examples or pointers? I believe I will have to run this in a State monad or the IO monad, will I not?
Have a look at the monte-carlo monad: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/monte-carlo For example usages see http://quantile95.com/2008/12/31/monte-carlo-poker-odds/ and http://randomdeterminism.wordpress.com/2009/01/06/monty-hall-problem-using-m... Aditya

Michael P Mossey wrote:
I have an application for random numbers. So far, in my journey with Haskell, I have learned basics and a few things about monads, and I was hoping I could get some guidance how to employ random # gens.
Can I get a few examples or pointers? I believe I will have to run this in a State monad or the IO monad, will I not?
Yes, the State StdGen monad for example. Note however that it's best to keep it abstract, i.e. to hide the fact that it happens to be a state monad. See also http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/ for making the case, http://en.wikibooks.org/wiki/Haskell/Understanding_monads for detailing the random number monad, and http://apfelmus.nfshost.com/random-permutations.html for an example use. Regards, apfelmus -- http://apfelmus.nfshost.com
participants (4)
-
Aditya Mahajan
-
Brandon S. Allbery KF8NH
-
Heinrich Apfelmus
-
Michael P Mossey