Skip to content

Instantly share code, notes, and snippets.

@billdozr
Created May 5, 2010 10:11
Show Gist options
  • Save billdozr/390600 to your computer and use it in GitHub Desktop.
Save billdozr/390600 to your computer and use it in GitHub Desktop.
import Control.Monad.State
import System.Random
main = do randNums <- runTwoRandoms
putStrLn $ "Random two numbers: " ++ show randNums
type RandomState a = State StdGen a
getRandom :: Random a => RandomState a
getRandom =
get >>= \gen ->
let (val, gen') = random gen in
put gen' >>
return val
getTwoRandoms :: Random a => RandomState (a, a)
getTwoRandoms = liftM2 (,) getRandom getRandom
runTwoRandoms :: IO (Int, Int)
runTwoRandoms = do
oldState <- getStdGen
let (result, newState) = runState getTwoRandoms oldState
setStdGen newState
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment