[Haskell-cafe] random library

William Yager will.yager at gmail.com
Wed Oct 11 01:30:34 UTC 2017

The best way to generate random values in multiple threads is to safely
split your random generator every time you fork.

In the case of mwc-random, it looks like you will want to do something like

gen' <- initialize =<< (uniformVector gen 32 :: IO (Vector Word32))
forkIO $ ... gen'
... gen

There is no good reason to share RNG state across threads. Just use a
separate RNG state for every thread. This is inherently thread-safe and
more performant.

If you're forking very frequently, you will want to benchmark the effect of
using a more efficient vector type (i.e. Vector.Unboxed instead of Vector)
or fewer elements during initialization.

On Tue, Oct 10, 2017 at 9:06 PM, Kazu Yamamoto <kazu at iij.ad.jp> wrote:

> Hi David,
> Thank you for your reply.
> > I've heard good things about *mwc-random* (https://github.com/bos/mwc-
> random
> > )
> Are there any example code to use mwc-random safely from multiple
> threads?
> // Kazu
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20171010/0609d927/attachment.html>

More information about the Haskell-Cafe mailing list