<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">
After thinking about this for a while, I don’t agree with with an associated type on RandomNumberGenerator. I think a generic FixedWidthInteger &amp; 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.&nbsp;</div>
<div name="messageSignatureSection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">
<br>
- Alejandro</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">
<br>
On Nov 12, 2017, 9:46 PM -0600, Xiaodi Wu &lt;xiaodi.wu@gmail.com&gt;, wrote:<br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;">
<div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto">On Sun, Nov 12, 2017 at 19:47 Alejandro Alonso &lt;<a href="mailto:aalonso128@outlook.com" target="_blank">aalonso128@outlook.com</a>&gt; wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
Sorry I’ve been gone for a while, I had to do a lot of traveling.
<div><br>
</div>
<div>1. Initially I made this thinking that developers had the power to determine their own lower bound. The current implementation uses the integer’s min value as a lower bound. If it makes sense to only allow unsigned integers from an RNG, then I’m perfectly
 fine with. I do disagree when you say that it should only generate UInt32s. The current approach allows, lets say mt19337 and mt19337-64, to be used within one generator. So if you wanted a UInt32, mt19337 would be used, and if you asked for a UInt64, mt19337-64
 would be used.</div>
</div>
</div>
</blockquote>
<div dir="auto"><br>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto">I agree with Nate that this doesn't need to be--and is better off not being--generic. The two different Mersenne Twisters are properly two distinct PRNGs. A user sufficiently sophisticated to ask for their own PRNG would expect the same algorithm
 to be used to generate a value of any bitwidth, by concatenation of successively generated random bits if necessary.</div>
<div dir="auto"><br>
</div>
<div dir="auto">Instead, the return type should be an associated type. 32-bit PRNG algorithms would always return values of type UInt32 and 64-bit algorithms would always return values of type UInt64.</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto"><br>
</div>
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<div></div>
<div>2. The Randomizable protocol isn’t always used with integers. Think Date.random or Color.random. These types of values are difficult to express with ranges. Randomizable solves this issue.</div>
<div><br>
</div>
<div>3. I’ve made the adjustment necessary for this.</div>
<div><br>
</div>
<div>4. So while I can see your point for this, it would break the consistency with Randomizable’s random property. You could argue that we could make this property a function itself, but I think most will agree that Int.random is a cleaner api than Int.random().&nbsp;</div>
<div><br>
</div>
<div>5. I’ve made the adjustment necessary for this.</div>
<div><br>
</div>
<div>6. I actually forgot to implement the random api for the ranges where Bound: BinaryFloatingPoint. While implementing this, I realized these would never fail and would always return a non-optional.</div>
</div>
</div>
</blockquote>
<div dir="auto"><br>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="gmail_quote"></div>
</div>
</div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto">Sure it can. 0.0..&lt;0.0 is an empty range, just like 0..&lt;0. Any collection type really should return an Optional for 'random' because collections can be empty.</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto"><br>
</div>
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<div>So, I decided making the other Countable ranges non-optional. (0 ..&lt; 10).random would return a non-optional, (0.0 ..&lt; 10.0).random would return a non-optional, and Array(0 ..&lt; 10).random would return an optional. I can agree that something like (0 ..&lt;
 10).random is hard to discover, so I added Int.random(in: 0 ..&lt; 10) (along with BinaryFloatingPoint).</div>
</div>
</div>
</blockquote>
<div dir="auto"><br>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto">This is unsatisfying (to me, at least). The proposed design should offer one very good way to accomplish something, not two spellings for the same thing.</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div class="gmail_quote">
<div dir="auto"><br>
</div>
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<div>However, these are not requirements of Randomizable. I think these methods would benefit more if they were extension methods:</div>
<div><br>
</div>
<div>extension Randomizable where Self: FixedWidthInteger, Self.Stride: SignedInteger {</div>
<div> public static func random(</div>
<div>  in range: Countable{Closed}Range,</div>
<div>  using generator: RandomNumberGenerator</div>
<div> ) -&gt; Self {</div>
<div>  return range.random(using: generator)</div>
<div> }<br>
</div>
<div>}</div>
<div><br>
</div>
<div>extension Randomizable where Self: BinaryFloatingPointer {</div>
<div> public static func random(<br>
</div>
<div>  in range: {Closed}Range,</div>
<div>  using generator: RandomNumberGenerator</div>
<div> ) -&gt; Self {</div>
<div>  return range.random</div>
<div> }<br>
</div>
<div>}</div>
<div><br>
</div>
<div>I think external types that wish to do something similar, like Data.random(bytes: 128), could extend Randomizable with their own custom needs. The stdlib would at this point provide all the features needed to make this happen very simply for something
 like Data.random(bytes: 128).</div>
</div>
</div>
<div>
<div name="messageSignatureSection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
- Alejandro</div>
</div>
<div>
<div name="messageReplySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
On Nov 5, 2017, 10:44 PM -0600, Nate Cook &lt;<a href="mailto:natecook@apple.com" target="_blank">natecook@apple.com</a>&gt;, wrote:<br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #3498db;">
Thanks for continuing to push this forward, Alejandro! I’m excited about the potential of having access to these APIs as part of the standard library. Here are a few comments on some different parts of the proposal:
<div><br>
</div>
<div>1) For your&nbsp;<font face="Menlo">RandomGenerator</font> protocol, I’m not totally clear on the semantics of the
<font face="Menlo">next(_:)</font> and <font face="Menlo">next(_:upperBound:)</font> methods. Do they both have zero as their lower bound, for example? I’m not sure it makes sense to have signed integers generated directly by an RNG—perhaps
<font face="Menlo">T: FixedWidthInteger &amp; UnsignedInteger</font> would be a more useful constraint. (Does it even need to be generic? What if RNGs just generate
<font face="Menlo">UInt32</font>s?)</div>
<div><br>
</div>
<div>2) Can you say more about the purpose of the <font face="Menlo">Randomizable</font> protocol? How would we use that protocol in useful ways that we wouldn’t get from being able to select random values from ranges (half-open and closed) of
<font face="Menlo">FixedWidthInteger</font> / <font face="Menlo">BinaryFloatingPoint</font>? My experience has been that a full-width random value is rarely what a user needs.</div>
<div><br>
</div>
<div>3) I agree with Xiaodi that <font face="Menlo">Random</font> should probably be a struct with a single shared instance, but I don’t think it should be internal. Hiding that shared RNG would make it hard for non-stdlib additions to have the same usage,
 as they would need to have completely separate implementations for the “default” and custom RNG versions.</div>
<div><br>
</div>
<div>4) I would also still suggest that the simplest version of <font face="Menlo">
random</font>&nbsp;(that you use to get a value from a range or an element from a collection) should be a function, not a property. Collection properties like
<font face="Menlo">first</font>, <font face="Menlo">last</font>, and <font face="Menlo">
count</font> all represent facts that already exist about a collection, and don’t change unless the collection itself changes. Choosing a random element, on the other hand, is clearly going to be freshly performed on each call. In addition, with the notable
 exception of <font face="Menlo">count</font>, we try to ensure O(1) performance for properties, while
<font face="Menlo">random</font> will be O(n) except in random-access collections. Finally, if it is a method, we can unify the two versions by providing a single method with the shared RNG as the default parameter.</div>
<div><br>
</div>
<div>5) To match the <font face="Menlo">sorted()</font> method, <font face="Menlo">
shuffled()</font> should be on <font face="Menlo">Sequence</font> instead of <font face="Menlo">
Collection</font>. I don’t think either <font face="Menlo">shuffled()</font> or <font face="Menlo">
shuffle()</font> needs to be a protocol requirement, since there isn’t really any kind of customization necessary for different kinds of collections. Like the sorting algorithms, both could be regular extension methods.
<div>
<div><br>
</div>
<div>6) I don’t know whether or not a consensus has formed around the correct spelling of the APIs for generating random values. From the proposal it looks like the preferred ways of getting a random value in a range would be to use the random property (or
 method) on a range or closed range:</div>
<div><br>
</div>
<div><font face="Menlo">&nbsp; &nbsp; (0..&lt;10).random &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 7</font></div>
<div><font face="Menlo">&nbsp; &nbsp; (0.0&nbsp;... 5.0).random &nbsp; &nbsp; // 4.112312</font></div>
<div><br>
</div>
<div>If that’s the goal, and we don’t want those values to be optional, we’ll need an implementation of random for floating-point ranges and an overload for fixed-width integer ranges. That said, I don’t think that style is as discoverable as having static
 methods or initializers available on the different types:</div>
<div><br>
</div>
<div><span style="font-family:Menlo">&nbsp; &nbsp; Int.random(in: 0..&lt;10)</span></div>
<div><span style="font-family:Menlo">&nbsp; &nbsp; Double.random(in: 0.0 ... 5.0)</span></div>
<div><font face="Menlo">&nbsp; &nbsp; // or maybe</font></div>
<div><font face="Menlo">&nbsp; &nbsp; Int(randomIn: 0..&lt;10)</font></div>
<div><font face="Menlo">&nbsp; &nbsp; Double(randomIn: 0.0 ... 5.0)</font></div>
<div><br>
</div>
<div>(My only quibble with the initializer approach is that <font face="Menlo">Bool</font> would be awkward.)</div>
<div><br>
</div>
<div>In addition, this alternative approach could make creating random values more consistent with types that don’t work well in ranges:</div>
<div><br>
</div>
<div>
<div><span style="font-family:Menlo">&nbsp; &nbsp; Data.random(bytes: 128)</span></div>
<div><span style="font-family:Menlo">&nbsp; &nbsp; Color.random(r: 0...0, g: 0...1, b: 0...1, a: 1...1)</span></div>
<div><br>
</div>
<div><span style="font-family:Menlo">————</span></div>
<div><span style="font-family:Menlo"><br>
</span></div>
</div>
<div>Thanks again!</div>
<div>Nate</div>
<div><br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #d35400;">
<div>On Nov 5, 2017, at 6:33 PM, Alejandro Alonso via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div>
<br class="m_-2411768158972897334m_-4738740749314821500m_7218634465431749671Apple-interchange-newline">
<div>
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<a href="https://github.com/apple/swift-evolution/pull/760" target="_blank">https://github.com/apple/swift-evolution/pull/760</a>&nbsp;is the current API and proposed solution.</div>
<div name="messageSignatureSection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
- Alejandro</div>
<div name="messageReplySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
On Nov 5, 2017, 6:18 PM -0600, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;, wrote:<br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #34495e;">
<div>
<div>My comments are directed to the &quot;more up-to-date&quot; document that you just linked to in your reply to Jon. Is that one outdated? If so, can you send a link to the updated proposal and implementation for which you're soliciting feedback?</div>
<br>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Sun, Nov 5, 2017 at 6:12 PM, Alejandro Alonso <span>&lt;<a href="mailto:aalonso128@outlook.com" target="_blank">aalonso128@outlook.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #2ecc71;">
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
The proposal and implementation have the current updated API. The link I sent Jon was the one I brought up a few weeks ago which is outdated now. The proposal answers all of your questions. As for `.random` being a function, some would argue that it behaves
 in the same way as `.first` and `.last` which are properties.</div>
<div class="m_-2411768158972897334m_-4738740749314821500m_7218634465431749671HOEnZb">
<font color="#888888"></font>
<div name="messageSignatureSection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<font color="#888888"><br>
- Alejandro</font></div>
</div>
<div>
<div class="m_-2411768158972897334m_-4738740749314821500m_7218634465431749671h5">
<div name="messageReplySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
On Nov 5, 2017, 6:07 PM -0600, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;, wrote:<br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #9b59b6;">
<div>A few quick thoughts:
<div><br>
</div>
<div>I know that there's been some discussion that `(1...10).random` is the best spelling, but I'd like to push back on that suggestion. When I want a random number, I tend to think of the type I want first (&quot;I want a random integer&quot;) and then a range (&quot;I want
 a random integer between a and b&quot;), not the other way around. My intuition is that `Int.random(in:)` will be more discoverable, both on that basis and because it is more similar to other languages' syntax (`Math.random` in JavaScript and `randint` in NumPy,<br>
for example). It also has the advantage that the type is explicit, which I think is particularly useful in this case because the value itself is, well, random.</div>
<div><br>
</div>
<div>I would also argue that, `random` is most appropriately a method and not a property; there's no hard and fast rule for this, but the fact that the result is stochastic suggests (to me) that it's not a &quot;property&quot; of the range (or, for that matter, of the
 type).</div>
<div><br>
</div>
<div>
<div>I would reiterate here my qualms about `Source` being the term used for a generator. These types are not a _source_ of entropy but rather a _consumer_ of entropy.</div>
</div>
<div><br>
</div>
<div>`UnsafeRandomSource` needs to be renamed; &quot;unsafe&quot; has a specific meaning in Swift--that is, memory safety, and this is not it. Moreover, it's questionable whether this protocol is useful in any sense. What useful generic algorithms can one write with
 such a protocol?</div>
<div><br>
</div>
<div>`XoroshiroRandom` cannot be seeded by any `Numeric` value; depending on the specific algorithm it needs a seed of a specific bit width. If you default the shared instance to being seeded with an `Int` then you will have to have distinct implementations
 for 32-bit and 64-bit platforms. This is unadvisable. On that note, your `UnsafeRandomSource` needs to have an associated type and not a generic `&lt;T : Numeric&gt;` for the seed.<br>
</div>
<div><br>
</div>
<div>The default random number generator should be cryptographically secure; however, it's not clear to me that it should be device random.</div>
<div><br>
</div>
<div>I agree with others that alternative random number generators other than the default RNG (and, if not default, possibly also the device RNG) should be accommodated by the protocol hierarchy but not necessarily supplied in the stdlib.</div>
<div><br>
</div>
<div>The term `Randomizable` means something specific which is not how it's used in your proposed protocol.&nbsp;</div>
<div><br>
</div>
<div>There's still the open question, not answered, about how requesting an instance of the hardware RNG behaves when there's insufficient or no entropy. Does it return nil, throw, trap, or wait? The proposed API does not clarify this point, although based
 on the method signature it cannot return nil or throw. Trapping might be acceptable but I'd be interested to hear your take as to why it is preferable.</div>
<div><br>
</div>
<div><br>
</div>
<div class="gmail_extra">
<div class="gmail_quote">On Sun, Nov 5, 2017 at 4:43 PM, Alejandro Alonso via swift-evolution
<span>&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e74c3c;">
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
For the proof of concept, I had accidentally deleted that one. I have a more up to date one which was discussed a few weeks later.&nbsp;<a href="https://gist.github.com/Azoy/15f0518df38df9b722d4cb17bafea4c1" target="_blank">https://gist.github.com/Azoy/15f0518df38df9b722d4cb17bafea4c1</a></div>
<div name="messageSignatureSection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
- Alejandro</div>
<div>
<div class="m_-2411768158972897334m_-4738740749314821500m_7218634465431749671m_1947579187318224822gmail-h5">
<div name="messageReplySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
On Nov 5, 2017, 4:37 PM -0600, Jonathan Hull &lt;<a href="mailto:jhull@gbis.com" target="_blank">jhull@gbis.com</a>&gt;, wrote:<br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;">
Is there a link to the writeup?&nbsp; The one in the quote 404s.
<div><br>
</div>
<div>Thanks,</div>
<div>Jon</div>
<div><br>
<div>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div>On Nov 5, 2017, at 2:10 PM, Alejandro Alonso via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div>
<br class="m_-2411768158972897334m_-4738740749314821500m_7218634465431749671m_1947579187318224822gmail-m_3739135728595962658Apple-interchange-newline">
<div>
<div>
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
Hello once again Swift evolution community. I have taken the time to write up the proposal for this thread, and have provided an implementation for it as well. I hope to once again get good feedback on the overall proposal.</div>
<div name="messageSignatureSection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
- Alejandro</div>
<div name="messageReplySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
On Sep 8, 2017, 11:52 AM -0500, Alejandro Alonso via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;, wrote:<br>
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #3498db;">
<div name="messageBodySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
Hello swift evolution, I would like to propose a unified approach to `random()` in Swift. I have a simple implementation here&nbsp;<a href="https://gist.github.com/Azoy/5d294148c8b97d20b96ee64f434bb4f5" target="_blank">https://gist.github.com/Azoy/5d294148c8b97d20b96ee64f434bb4f5</a>.
 This implementation is a simple wrapper over existing random functions so existing code bases will not be affected. Also, this approach introduces a new random feature for Linux users that give them access to upper bounds, as well as a lower bound for both
 Glibc and Darwin users. This change would be implemented within Foundation.
<div><br>
</div>
<div>I believe this simple change could have a very positive impact on new developers learning Swift and experienced developers being able to write single random declarations.</div>
<div><br>
</div>
<div>I’d like to hear about your ideas on this proposal, or any implementation changes if need be.</div>
<div><br>
</div>
<div>- Alejando</div>
</div>
<div name="messageReplySection" style="font-size:14px;font-family:-apple-system,BlinkMacSystemFont,sans-serif">
<br>
<div></div>
</div>
</blockquote>
</div>
</div>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div>
</blockquote>
</div>
<br>
</div>
</blockquote>
</div>
</div>
</div>
</div>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</blockquote>
</div>
</div>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</body>
</html>