In an attempt to learn Haskell better, I'm trying to write a program that displays the average value of the sum of 2 die, rolled X number of times. This is fairly simple in C, Java, Python... but I'm stuck in Haskell. Here's a naive attempt:
import System.Random
main = do
g <- getStdGen
let trials = 10000000
let rolls = take trials (randomRs (2, 12) g :: [Int])
let average = div (sum rolls) trials
print average
For low number of trials, the program works. But when I run this code with ten million trials, I get an error:
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.
There's got to be a better way to write this program. In the C, Java, and Python versions, this is a simple task. I've looked at this post (and understand about 75% of the material), but when I adapt that code to this situation, summing a sequence of R [Int]
doesn't work (and I'm not sure how to 'unwrap' the [Int]). What am I doing wrong? What's the right way? How do I reach random number enlightenment in Haskell?
Edit: in addition to the answer selected, as rtperson points out below, the modeling of 2 dice is incorrect; it should really be the sum of two independent rolls from 1 to 6.
sum
is no good to sum a long list, it runs in linear space. Try this strict version of sum
:
sum' = foldl' (+) 0
foldl'
is defined in Data.List
.
EDIT More information can be found in this HaskellWiki article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With