[Haskell-cafe] Performance problem with random numbers

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Oct 13 13:35:32 EDT 2007


On Oct 13, 2007, at 13:30 , ntupel wrote:

> On Sat, 2007-10-13 at 12:42 -0400, Brandon S. Allbery KF8NH wrote:
>> Your apparently simple StdGen argument is actually a sort of program
>> state (represented by unevaluated thunks, not by a state monad; see
>> below) which gets altered with every invocation of random.  If
>> nothing is forced until the very end, it in effect becomes an
>> expression which produces the desired StdGen, with the uses of the
>> previous StdGen values as "side effects" of its computation that
>> occur when the thunk is evaluated at the end.  I'm not sure I'm up to
>> working through an example of what this looks like.
>
> Thanks Brandon. I understand your argument but I don't know how to put
> it into practice, i.e. how to force the evaluation of StdGen.

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.

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.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH




More information about the Haskell-Cafe mailing list