[Haskell-cafe] Generate random UArray in constant memory space.

Vasyl Pasternak vasyl.pasternak at gmail.com
Tue Feb 9 07:18:23 EST 2010

Hello Cafe,

I tried to generate memory-efficient list of random numbers, so I've
used uvector library for this task. But it doesn't work,
it still allocates more than 6Gb of memory for the random list of 10
million elements. Here is the code:

> import Text.Printf
> import System.Random
> import Control.Applicative
> import System.Environment
> import Data.Array.Vector

> randomListU :: (Int, Int) -> StdGen -> Int -> (UArr Int)
> randomListU b g size = unfoldU size gen g
>   where gen g = let (x, g') = randomR b g
>                 in JustS (x :*: g')

> main = do
>   [size] <- map read <$> getArgs
>   let ints = randomListU (-10, 10) (mkStdGen 1) size
>   printf "%d\n"  (sumU ints)

Could someone give a hint, how to implement this function in constant
memory space?

Thank you in advance.

Best regards,
Vasyl Pasternak

More information about the Haskell-Cafe mailing list