Improving Random

dominic at steinitz.org dominic at steinitz.org
Wed May 27 06:04:27 UTC 2020


Hi Zemyla,

It’s fully documented in the haddocks which I have temporarily put here: https://htmlpreview.github.io/?https://raw.githubusercontent.com/idontgetoutmuch/random/haddock-preview/doc/index.html <https://htmlpreview.github.io/?https://raw.githubusercontent.com/idontgetoutmuch/random/haddock-preview/doc/index.html>. There was a link to them in the announcement but maybe I should have highlighted it a bit more.

Specifically suppose you love permuted congruential generators then

> data PCGen = PCGen !Word64 !Word64
> 
> stepGen :: PCGen -> (Word32, PCGen)
> stepGen (PCGen state inc) = let
>   newState = state * 6364136223846793005 + (inc .|. 1)
>   xorShifted = fromIntegral (((state `shiftR` 18) `xor` state) `shiftR` 27) :: Word32
>   rot = fromIntegral (state `shiftR` 59) :: Word32
>   out = (xorShifted `shiftR` (fromIntegral rot)) .|. (xorShifted `shiftL` fromIntegral ((-rot) .&. 31))
>   in (out, PCGen newState inc)
> 
> instance RandomGen PCGen where
>   genWord32 = stepGen
>   split _ = error "PCG is not splittable”

And if you prefer a monadic style interface:

> > (/ 1000) $ sum $ runStateGen_ (PCGen 17 29) $ \g -> replicateM 1000 $ uniformRM (0.0, 1.0) g
> 
> 0.508595082944005


HTH - let me know if you have any other questions.

Dominic Steinitz
dominic at steinitz.org
http://idontgetoutmuch.org
Twitter: @idontgetoutmuch

> On 26 May 2020, at 23:47, Zemyla <zemyla at gmail.com> wrote:
> 
> And can you explain how to take an existing RNG and write it in this new format?
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20200527/026f9585/attachment.html>


More information about the Libraries mailing list