[Haskell-cafe] Performance problem with random numbers

ntupel ntupel at googlemail.com
Sat Oct 13 14:59:15 EDT 2007

On Sat, 2007-10-13 at 13:35 -0400, Brandon S. Allbery KF8NH wrote:
> For starters, look into "seq". Try applying it to any expression  
> using a generated random number.  This should force evaluation to  
> occur somewhere other than when random is trying to figure out what  
> StdGen value it's been told to use as its initial state.

Ok, but I still wonder where that might be. random and randomR are used
in a function named "next" as show here:

next :: (Array Int a, Array Int a, UArray Int Double) -> StdGen -> (a,
next (xs, as, rs) g =
    let n = length $ indices xs
        (x1, g1) = randomR (0, n - 1) g
        (x2, g2) = random g1
        r = rs!x1
        if x2 <= r 
            then (xs!x1, g2) 
            else (as!x1, g2)

x1 and x2 are used in the same function so I assume this already
requires their evaluation. The only function that calls "next" is

randomList :: (Array Int a, Array Int a, UArray Int Double) -> StdGen ->
randomList t g = 
    let (n, g') = next t g
        n:randomList t g'

Cf. my original e-mail for the complete program. 

> Alternately you can put all the uses in IO and use  
> Control.Exception.evaluate (or even print).  This is probably not  
> what you want to do in your actual production code, however.

Right. This is not what I want.

Many thanks again,

More information about the Haskell-Cafe mailing list