I'm working with the packages System.Random.Mersenne.Pure64 and Control.Monad.Mersenne.Random by Don Stewart which are usually blazingly fast, and are supposed to help avoid common errors, like using non-strict state monads.
Nevertheless, I managed to write some code that results in a stack overflow for moderately large vectors.
import qualified Data.Vector.Unboxed as U
import Data.Int
import System.Random.Mersenne.Pure64
import Control.Monad.Mersenne.Random
main = do
let dim = 1000000
y = evalRandom (U.replicateM dim getInt64) (pureMT 13) :: U.Vector Int64
putStr $ (show $ U.head y)
I'm guessing this must be due to laziness in Vector's replicateM
implementation, though it's difficult to see since it is implemented using streams
.
How might I write code that uses constant stack space to sample large vectors?