Demonstrate how to carry to implicit states with Monads :

import System.Random

import Control.Monad.State

type RandomState = State StdGen

getRandom ::(Random a) => RandomState a

getRandom = get >>= \g ->

let (a,g’) = random g

in put g’ >> return a

twoRands ::(Random a,Random b)=> RandomState (a,b)

twoRands = liftM2 (,) getRandom getRandom

getRandoms :: (Random a) =>RandomState [a]

getRandoms = getRandom >>= \a -> getRandoms >>= (\xs -> return (a:xs))

fullRands rand= getStdRandom (runState rand)

getRandom’ ::(Random a) => RandomState a

getRandom’ = do

g RandomState (a,b)

twoRands’ = do

a<-getRandom'

b RandomState (a,b)

twoRands” = getRandom >>= \a->

getRandom >>= \b -> return (a,b)

threeRands :: (Random a,Random b,Random c)=> RandomState (a,b,c)

threeRands = do

(a,b) <- twoRands

c<- getRandom

return (a,b,c)