<div dir="ltr">The best way to generate random values in multiple threads is to safely split your random generator every time you fork.<div><br></div><div>In the case of mwc-random, it looks like you will want to do something like </div><div><br></div><div>gen' <- initialize =<< (uniformVector gen 32 :: IO (Vector Word32))</div><div>forkIO $ ... gen'</div><div>... gen</div><div><br></div><div>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.</div><div><br></div><div>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. </div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 10, 2017 at 9:06 PM, Kazu Yamamoto <span dir="ltr"><<a href="mailto:kazu@iij.ad.jp" target="_blank">kazu@iij.ad.jp</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi David,<br>
<br>
Thank you for your reply.<br>
<br>
> I've heard good things about *mwc-random* (<a href="https://github.com/bos/mwc-random" rel="noreferrer" target="_blank">https://github.com/bos/mwc-<wbr>random</a><br>
> )<br>
<br>
Are there any example code to use mwc-random safely from multiple<br>
threads?<br>
<div class="HOEnZb"><div class="h5"><br>
// Kazu<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</div></div></blockquote></div><br></div>