Skip to content

Instantly share code, notes, and snippets.

@hardentoo
Forked from ijt/random_monad_example.hs
Created July 12, 2018 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hardentoo/f3e5a15a85c2ef186747b9ab0bd4c381 to your computer and use it in GitHub Desktop.
Save hardentoo/f3e5a15a85c2ef186747b9ab0bd4c381 to your computer and use it in GitHub Desktop.
Example of how to use the Random monad in Haskell
-- 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