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
Andriy Drozdyuk
61.5k51 gold badges178 silver badges279 bronze badges
2 Answers 2
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
porges
30.7k4 gold badges88 silver badges115 bronze badges
Sign up to request clarification or add additional context in comments.
11 Comments
dflemstr
I think you have
runRand and evalRand mixed up!porges
Not sure how that happened, since I ran the code :) Fixed now.
pat
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.porges
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).pat
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. |
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
dflemstr
26.3k5 gold badges74 silver badges107 bronze badges
3 Comments
Andriy Drozdyuk
Also... what would happen if I called
func not from main?dflemstr
do doesn't mean that you use the IO monad. I'm using the Rand monad here.dflemstr
func can be called from any other function in the Rand monad. I'll update with an example.lang-hs