<div dir="ltr">Thank you for all the feedback and options. In further investigation, I also ran across the family of xx48 rngs, which appear to be on both platforms. They claim to be uniformly distributed.<div><br></div><div><span style="color:rgb(0,0,0);font-family:&quot;Times New Roman&quot;">drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, seed48, srand48</span><br></div><div><span style="color:rgb(0,0,0);font-family:&quot;Times New Roman&quot;"><br></span></div><div><font color="#000000" face="Times New Roman"><a href="http://pubs.opengroup.org/onlinepubs/007908775/xsh/drand48.html">http://pubs.opengroup.org/onlinepubs/007908775/xsh/drand48.html</a></font><br></div><div><font color="#000000" face="Times New Roman"><br></font></div><div><font color="#000000" face="Times New Roman">Any reason not to use one of these instead?</font></div><div><font color="#000000" face="Times New Roman"><br></font></div><div><font color="#000000" face="Times New Roman">Thanks, Ed</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 23, 2017 at 3:18 AM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Here&#39;s a stripped down verison of my implementation of the Xoroshiro128+ PRNG:<div><a href="https://gist.github.com/anonymous/527602968812f853d6147aea8c66d660" target="_blank">https://gist.github.com/<wbr>anonymous/<wbr>527602968812f853d6147aea8c66d6<wbr>60</a></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>/Jens</div><div><br></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 22, 2017 at 11:21 PM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Sorry for the premature send ...<div>Here is the site: <a href="http://xoroshiro.di.unimi.it" target="_blank">http://xoroshiro.di.unimi.it</a><br></div><div>There is also a section there about &quot;generating uniform doubles in unit interval&quot; which is worth reading.</div><div>And here&#39;s how to get uniform floating point values in the range [0, 1) from various (supposedly) random bit patterns:</div><div><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(4,51,255)"><span style="font-variant-ligatures:no-common-ligatures">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt64</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> shifts: </span><span style="font-variant-ligatures:no-common-ligatures">UInt64</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> = 63 - </span><span style="font-variant-ligatures:no-common-ligatures">UInt64</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">Double</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures">significandBitCo<wbr>unt</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(v &gt;&gt; shifts) * (.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">ulpOfOne</span><span style="font-variant-ligatures:no-common-ligatures">/2)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt32</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(v) + 0.5) / (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt32</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">max</span><span style="font-variant-ligatures:no-common-ligatures">) + 1.0)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt16</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(v) + 0.5) / (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt16</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">max</span><span style="font-variant-ligatures:no-common-ligatures">) + 1.0)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(v) + 0.5) / (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Double</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">max</span><span style="font-variant-ligatures:no-common-ligatures">) + 1.0)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(4,51,255)"><span style="font-variant-ligatures:no-common-ligatures">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt64</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> shifts: </span><span style="font-variant-ligatures:no-common-ligatures">UInt64</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> = 63 - </span><span style="font-variant-ligatures:no-common-ligatures">UInt64</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">Float</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures">significandBitCou<wbr>nt</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(v &gt;&gt; shifts) * (.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">ulpOfOne</span><span style="font-variant-ligatures:no-common-ligatures">/2)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt32</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> shifts: </span><span style="font-variant-ligatures:no-common-ligatures">UInt32</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> = 31 - </span><span style="font-variant-ligatures:no-common-ligatures">UInt32</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">Float</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures">significandBitCou<wbr>nt</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(v &gt;&gt; shifts) * (.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">ulpOfOne</span><span style="font-variant-ligatures:no-common-ligatures">/2)</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt16</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> a = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(v) + 0.5</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> b = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt16</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">max</span><span style="font-variant-ligatures:no-common-ligatures">) + 1.0</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = a / b</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">init</span><span style="font-variant-ligatures:no-common-ligatures">(unitRange v: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> a = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(v) + 0.5</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> b = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Float</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">max</span><span style="font-variant-ligatures:no-common-ligatures">) + 1.0</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">self</span><span style="font-variant-ligatures:no-common-ligatures"> = a / b</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">You will get a very fast and good quality prng using xoroshiro, converting to unit range floating point and then back to uniform range int if you want to, much much faster than arc4random.</span></p><span class="m_2744094100351814297HOEnZb"><font color="#888888"><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">/Jens</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p></font></span></div></div><div class="m_2744094100351814297HOEnZb"><div class="m_2744094100351814297h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 22, 2017 at 11:17 PM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Check out the generators (especially xoroshiro) on this site:</div><div class="m_2744094100351814297m_-8876985151572480040HOEnZb"><div class="m_2744094100351814297m_-8876985151572480040h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 22, 2017 at 6:54 PM, Saagar Jha via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">Saagar Jha</div>

</div>
<br><div><span><blockquote type="cite"><div>On May 22, 2017, at 08:44, Edward Connell via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986Apple-interchange-newline"><div><div dir="ltr">Any ideas when Foundation on Linux will support arc4random_uniform? This is kind of an important function.<div>There doesn&#39;t seem to be any decent substitute without requiring the installation of libbsd-dev, which turns out to be messy. Currently I am doing this, but glibc random with mod does not produce good quality numbers, due to modulo bias.</div></div></div></blockquote><div><br></div></span><div>Modulo bias is easy to deal with, though, if you force random to produce a range that is a multiple of the range that you’re trying to produce:</div><span><div><br></div><div>guard range &gt; 0 else { return 0 }</div></span><div>var random: Int</div><div>repeat {</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986Apple-tab-span" style="white-space:pre-wrap">        </span>random = Int(random())</div><div>} while(random &gt; LONG_MAX / range * range)</div><div>return random % range</div><br><blockquote type="cite"><div><span><div dir="ltr"><div><div><br></div><div>Has anyone come up with a better solution to get a true uniform distribution that isn&#39;t super messy?</div><div> <div><div>import Foundation</div><div><br></div><div>#if os(Linux)</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>import Glibc</div><div>#endif</div></div><div><br></div><div><br></div><div><div>public func random_uniform(range: Int) -&gt; Int {</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>guard range &gt; 0 else { return 0 }</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>#if os(Linux)</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>  return Int(random()) % range</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>#else</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">          </span>return Int(arc4random_uniform(UInt32(<wbr>range)))</div><div><span class="m_2744094100351814297m_-8876985151572480040m_2482228478634656447m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>#endif</div><div>}</div></div><div><br></div></div><div><br></div><div>Thanks, Ed</div></div></div></span><span>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br></span></div></blockquote></div><br></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>