<div dir="ltr">Check out the generators (especially xoroshiro) on this site:</div><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"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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 class=""><blockquote type="cite"><div>On May 22, 2017, at 08:44, Edward Connell via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_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'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 class=""><div><br></div><div>guard range > 0 else { return 0 }</div></span><div>var random: Int</div><div>repeat {</div><div><span class="m_1244819390521955986Apple-tab-span" style="white-space:pre-wrap">        </span>random = Int(random())</div><div>} while(random > LONG_MAX / range * range)</div><div>return random % range</div><br><blockquote type="cite"><div><span class=""><div dir="ltr"><div><div><br></div><div>Has anyone come up with a better solution to get a true uniform distribution that isn't super messy?</div><div> <div><div>import Foundation</div><div><br></div><div>#if os(Linux)</div><div><span class="m_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) -> Int {</div><div><span class="m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>guard range > 0 else { return 0 }</div><div><span class="m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>#if os(Linux)</div><div><span class="m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span> return Int(random()) % range</div><div><span class="m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>#else</div><div><span class="m_1244819390521955986gmail-Apple-tab-span" style="white-space:pre-wrap">         </span>return Int(arc4random_uniform(UInt32(<wbr>range)))</div><div><span class="m_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 class="">
______________________________<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/<wbr>mailman/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">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>