<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Saagar Jha</div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On May 22, 2017, at 08:44, Edward Connell via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Any ideas when Foundation on Linux will support arc4random_uniform? This is kind of an important function.<div class="">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 class=""></div><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><div><br class=""></div><div>guard range > 0 else { return 0 }</div><div>var random: Int</div><div>repeat {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>random = Int(random())</div><div>} while(random > LONG_MAX / range * range)</div><div>return random % range</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div><div class="">Has anyone come up with a better solution to get a true uniform distribution that isn't super messy?</div><div class=""> <div class=""><div class="">import Foundation</div><div class=""><br class=""></div><div class="">#if os(Linux)</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>import Glibc</div><div class="">#endif</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class="">public func random_uniform(range: Int) -> Int {</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>guard range > 0 else { return 0 }</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>#if os(Linux)</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span> return Int(random()) % range</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>#else</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">         </span>return Int(arc4random_uniform(UInt32(range)))</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>#endif</div><div class="">}</div></div><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">Thanks, Ed</div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>