[Haskell-cafe] Splittable random numbers

Ryan Newton rrnewton at gmail.com
Sat Jan 22 06:40:04 CET 2011

On Wed, Nov 10, 2010 at 11:33 AM, Lauri Alanko <la at iki.fi> wrote:
> So a naive implementation of split would be:
> split g = (mkGen seed, g')
>  where (seed, g') = random g

Just to be clear, that is the same as Burton Smith's original proposal that
Simon mentioned at the outset, right?  Specifically, Smith's proposal is
yours instantiated with a crypto based PRNG?

So, except for the silliness of generating 128 random bits to make an Int,
the following would implement the strategy using the "crypto" package on
Hackage, correct?
import Codec.Encryption.AES
import Data.LargeWord
import System.Random

data RNG = RNG Word128 Word128 deriving Show
next128 (RNG k c) = (encrypt k c, RNG k (c+1))

instance RandomGen RNG where
  next g = (fromIntegral n, g')
   where (n,g') = next128 g
  split g@(RNG k c) = (g', RNG n 0)
   where (n,g') = next128 g

The reason I brought up AES-NI was that doing AES in hardware would allow
about an 8X improvement over the best software implementation (~2 cycles per
byte encrypted).  Comparison would be warranted, but perhaps it could make
the crypto based PRNG efficient enough for day-to-day use.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110122/547e164a/attachment.htm>

More information about the Haskell-Cafe mailing list