<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Nov 13, 2017, at 7:38 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">On Mon, Nov 13, 2017 at 7:12 PM, Alejandro Alonso <span dir="ltr" class=""><<a href="mailto:aalonso128@outlook.com" target="_blank" class="">aalonso128@outlook.com</a>></span> wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div class="">
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif" class="">
After thinking about this for a while, I don’t agree with with an associated type on RandomNumberGenerator. I think a generic FixedWidthInteger & UnsignedInteger should be sufficient. If there were an associated type, and the default for Random was UInt32,
then there might be some arguments about allowing Double to utilize the full 64 bit precision. We could make Random32 and Random64, but I think people will ask why there isn’t a Random8 or Random16 for those bit widths. The same could also be said that any
experienced developer would know that his PRNG would be switched if he asked for 32 bit or 64 bit. </div></div></blockquote><div class=""><br class=""></div><div class="">I don't understand. Of course, Double would require 64 bits of randomness. It would obtain this by calling `next()` as many times as necessary to obtain the requisite number of bits.</div><div class=""><br class=""></div><div class="">At base, any PRNG algorithm yields some fixed number of bits on each iteration. You can certainly have a function that returns an arbitrary number of random bits (in fact, I would recommend that such an algorithm be a protocol extension method on RandomNumberGenerator), but it must be built on top of a function that returns a fixed number of bits, where that number is determined on a per-algorithm basis. Moreover--and this is important--generating a random unsigned integer of arbitrary bit width in a sound way is actually subtly _different_ from generating a floating-point value of a certain bit width, and I'm not sure that one can be built on top of the other. Compare, for example:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/xwu/NumericAnnex/blob/c962760bf974a84ec57d8c5e94c91f06584e2453/Sources/PRNG.swift#L157" class="">https://github.com/xwu/NumericAnnex/blob/c962760bf974a84ec57d8c5e94c91f06584e2453/Sources/PRNG.swift#L157</a><br class=""></div><div class=""><a href="https://github.com/xwu/NumericAnnex/blob/c962760bf974a84ec57d8c5e94c91f06584e2453/Sources/PRNG.swift#L316" class="">https://github.com/xwu/NumericAnnex/blob/c962760bf974a84ec57d8c5e94c91f06584e2453/Sources/PRNG.swift#L316</a><br class=""></div><div class=""><br class=""></div><div class="">(These are essentially Swift versions of C++ algorithms.)</div><div class=""><br class=""></div><div class="">Basically, what I'm saying is that RandomNumberGenerator needs a `next()` method that returns a fixed number of bits, and extension methods that build on that to return T : FixedWidthInteger & UnsignedInteger of arbitrary bit width or U : BinaryFloatingPoint of an arbitrary number of bits of precision. Each individual RNG does not need to reimplement the latter methods, just a method to return a `next()` value of a fixed number of bits. You are welcome to use my implementation.</div></div></div></div></div></blockquote><div><br class=""></div><div>An alternative to this is to have the random generator write a specified number of bytes to a pointer’s memory, as David Waite and others have suggested. This is same way arc4random_buf and SecRandomCopyBytes are implemented. Each random number generator could then choose the most efficient way to provide the requested number of bytes. The protocol could look something like this:</div><div><div class=""><font color="#24292e" class=""><br class=""><font face="Menlo" class="">protocol RandomNumberGenerator {<br class=""> /// Writes the specified number of bytes to the given pointer’s memory.<br class=""> func read(into p: UnsafeMutableRawPointer, bytes: Int)<br class="">}</font></font></div><div class=""><font color="#24292e" face="HelveticaNeue" class=""><br class=""></font></div><div class=""><font color="#24292e" face="HelveticaNeue" class="">This is less user-friendly than having a next() method, but I think that’s a good thing—we very much want people who need a random value to use higher-level APIs and just pass the RNG as a parameter when necessary.</font></div><div class=""><font color="#24292e" face="HelveticaNeue" class=""><br class=""></font></div><div class=""><font color="#24292e" face="HelveticaNeue" class="">Nate</font></div><div class=""><font color="#24292e" face="HelveticaNeue" class=""><br class=""></font></div></div></div><br class=""></body></html>