Created
May 5, 2010 10:11
-
-
Save billdozr/390600 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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