Created
October 3, 2011 00:13
-
-
Save ijt/1258156 to your computer and use it in GitHub Desktop.
Example of how to use the Random monad in Haskell
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
-- cabal install MonadRandom | |
-- ghc random_monad_example | |
-- ./random_monad_example | |
-- The code here is stolen from a comment in the MonadRandom source code. | |
import Control.Monad.Random | |
die :: RandomGen g => Rand g Int | |
die = getRandomR (1,6) | |
dice :: RandomGen g => Int -> Rand g [Int] | |
dice n = sequence (replicate n die) | |
main = do | |
rolls <- evalRandIO $ dice 10 | |
print rolls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment