<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="">On Oct 4, 2017, at 7:26 PM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><div><blockquote type="cite" class="">* Remove the range-less function. Naive users often write things like `Int.random() % 100`, which unbeknownst to them is biased. Providing only the ranged interface nudges naive users toward correct usage. <br class=""><div class=""><div class=""><div class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><br class=""></div><div class="">* Provide an "easy" way to get some random bytes instead of a random number. Perhaps a Data initializer that returns random-filled bytes of the requested length. This helps make up for the lack of a range-less function on FixedWidthInteger.</div><div class=""><br class=""></div><div class="">The "easy" functions should get the best names: Int.random(in:), Data.random(length:), etc. The fundamental CSPRNG interface should have an interface that is less friendly and less discoverable.</div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""></div></div></blockquote><div dir="auto" class=""><br class=""></div></div></div><div class=""><div class="gmail_quote"><div dir="auto" class="">Agree, this is a very tractable set of functions for an initial implementation. In fact, with these primitives and maybe some shuffling and choosing conveniences in the stdlib, I see the remainder as useful-to-haves that may or may not be critical for inclusion in the stdlib vs more appropriate for a dedicated math library (more to follow on that thought in a little while).</div></div></div></blockquote><div dir="auto" class=""><br class=""></div></div></div></div><div class=""><div class=""><div class="gmail_quote"><div dir="auto" class="">To sum up my thoughts so far in code, building on previous comments from others, this would be a nice set of random APIs, IMO:</div><div dir="auto" class=""><br class=""></div><div dir="auto" class="">```</div><div dir="auto" class="">extension Int {</div><div dir="auto" class=""> static func random(in range: Countable{Closed}Range<Int>) -> Int</div><div dir="auto" class="">}</div></div></div></div></div></blockquote><div><br class=""></div><div>Nice. Should these be initializers like:</div><div><br class=""></div><div>extension Int {</div><div> init(randomIn: Countable{Closed}Range<Int>)</div><div>}</div><div><br class=""></div><div>or some other spelling?</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><div class=""><div class="gmail_quote"><div dir="auto" class="">extension Data {</div><div dir="auto" class=""> static func random(byteCount: Int) -> Data</div><div dir="auto" class="">}</div></div></div></div></div></blockquote><div><br class=""></div><div>Similarly:</div><div><br class=""></div><div>extension Data {</div><div> init(randomByteCount: Int)</div><div>}</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><div class=""><div class="gmail_quote"><div dir="auto" class=""><br class=""></div><div dir="auto" class="">extension UnsafeMutableRawPointer {</div><div dir="auto" class=""> func copyRandomBytes(count: Int) throws</div><div dir="auto" class="">}</div><div dir="auto" class="">// This function is to be the most primitive of the random APIs, and will throw if there is insufficient entropy.</div></div></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div class=""><div class=""><div class="gmail_quote"><div dir="auto" class=""><br class=""></div><div dir="auto" class="">extension UnsafeMutableRawBufferPointer {</div><div dir="auto" class=""> func copyRandomBytes() throws</div><div dir="auto" class="">}</div><div dir="auto" class="">// Just as UMRBP.copyBytes(from:) parallels UMRP.copyBytes(from:count:), we offer this convenience here.</div><div dir="auto" class="">```</div></div></div></div></div></blockquote><br class=""></div><div>I’m not sure what the semantics of these APIs are, but with the appropriate doc comment they’re probably great :-)</div><div><br class=""></div><div>-Chris</div><div><br class=""></div><br class=""></body></html>