[Haskell-cafe] In relation to shuffling

Alastair Reid alastair at reid-consulting-uk.ltd.uk
Fri Jul 9 09:51:46 EDT 2004


[Warning: this message is going to talk about 'splitting supplies' and 
'splitting random number generators'.  The two sound quite similar and can be 
used to accomplish similar goals but are quite different in the way you write 
your code, in how repeatable the results, etc. so try not to confuse the 
two.]

> Except that AFAICS, getStdGen gives you _the_ standard PRNG which
> means that you shouldn't use it (the standard PRNG) anymore afterwards,
> or you'll get repeated numbers:
> [...]
> newStdGen splits the current state (using one as the new stdGen and
> returning the other), which probably _is_ what you want.

Yes, you're quite right - use newStdGen instead of getStdGen.

Also consider use of a 'splittable supply'.  This is a generic mechanism for 
splitting infinite lists of values into trees of lists in situations where 
the values are regarded as 'equivalent' in the sense that they are all 
equally good.  For example, the fresh names used inside a compiler to name 
temporary variables are all equally good (as long as you don't reuse them), 
and random numbers are all equally good.  One Supply implementation is 
documented here: 

  http://www.csg.lcs.mit.edu/~earwig/haskell-lib/

but I think another was mentioned recently on one of the Haskell mailing 
lists.

> Except that I have no idea what hidden costs splitting random number
> generators have :) (anyone?)

A quick glance at the source suggests that the standard generator type is a 
pair of Ints and that the cost of splitting is a few arithmetic instructions 
- no big deal.

--
Alastair Reid


More information about the Haskell-Cafe mailing list