16

Can someone provide "for-dummies" example of how to use `MonadRandom'?

Currently I have code that does stuff like passing around the generator variable, all the way from the main function:

 main = do
 g <- getStdGen
 r <- myFunc g
 putStrLn "Result is : " ++ show r
 --my complicated func
 myFunc g x y z = afunc g x y z
 afunc g x y z = bfunc g x y
 bfunc g x y = cfunc g x
 cfunc g x = ret where
 (ret, _ ) = randomR (0.0, 1.0) g

Thanks

asked Feb 13, 2012 at 19:34

2 Answers 2

19

Basically all the extra g parameters can just be dropped. You then get random numbers using the functions from Control.Monad.Random (such as getRandomR). Here is your example (I added some args to make it compile):

import Control.Monad.Random
main = do
 g <- getStdGen
 let r = evalRand (myFunc 1 2 3) g :: Double
 -- or use runRand if you want to do more random stuff:
 -- let (r,g') = runRand (myFunc 1 2 3) g :: (Double,StdGen)
 putStrLn $ "Result is : " ++ show r
--my complicated func
myFunc x y z = afunc x y z
afunc x y z = bfunc x y
bfunc x y = cfunc x
cfunc x = do
 ret <- getRandomR (0.0,1.0)
 return ret
answered Feb 13, 2012 at 19:46
Sign up to request clarification or add additional context in comments.

11 Comments

I think you have runRand and evalRand mixed up!
Not sure how that happened, since I ran the code :) Fixed now.
Of course, myFunc, aFunc, bFunc, and cFunc are now monadic. They return values wrapped in the Rand monad, which have to be extracted with >>= (or <- in a do), and created with return.
No, you can use them from anywhere. You just need to use evalRand or runRand with a RandGen argument. Basically, you've changed from having to pass the argument around explicitly to getting that 'for free', but working inside a monad. It's also harder to make mistakes this way (like forgetting to pass on the second argument returned by randomR).
Remember, monadic doesn't imply the IO monad. In this case, we're talking about the Rand monad. It makes sure that the generator is threaded through your code correctly, so you don't inadvertently create two random numbers from the same state of the generator (which would generate two identical so-called random numbers). In your example, you are discarding the new generator. If you wanted to create a second random number, you might be tempted to use g again, but that would be incorrect.
|
10

You just run something in the RandT monad transformer with runRandT or evalRandT, and for the pure Rand monad, with runRand or evalRand:

main = do
 g <- getStdGen
 r = evalRand twoEliteNumbers g
 putStrLn $ "Result is: " ++ show r
twoEliteNumbers :: (RandomGen g) => Rand g (Double, Double)
twoEliteNumbers = do
 -- You can call other functions in the Rand monad
 number1 <- eliteNumber
 number2 <- eliteNumber
 return $ (number1, number2)
eliteNumber :: (RandomGen g) => Rand g Double
eliteNumber = do
 -- When you need random numbers, just call the getRandom* functions
 randomNumber <- getRandomR (0.0, 1.0)
 return $ randomNumber * 1337
Andreas Kahler
2,3631 gold badge18 silver badges17 bronze badges
answered Feb 13, 2012 at 19:41

3 Comments

Also... what would happen if I called func not from main?
do doesn't mean that you use the IO monad. I'm using the Rand monad here.
func can be called from any other function in the Rand monad. I'll update with an example.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.