[swift-evolution] [Proposal] Random Unification
erica at ericasadun.com
Fri Dec 1 12:04:49 CST 2017
> On Nov 30, 2017, at 5:21 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> On Thu, Nov 30, 2017 at 5:24 PM, Nate Cook <natecook at apple.com <mailto:natecook at apple.com>> wrote:
>> On Nov 30, 2017, at 4:30 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> On Thu, Nov 30, 2017 at 3:58 PM, Dave DeLong via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> On Nov 30, 2017, at 2:48 PM, Jonathan Hull via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> I would personally go with:
>>> Int.random //Returns a random Int
>> “Type.random” is so rarely used as to not be worth the addition, IMO. If you really need a random element from the *entire* domain, then I think you should have to manually create the ClosedRange<T> yourself.
>>> Int.random(in: ClosedRange<Int>) //Works for Comparable types. Gives a result from the closed range. Closed Range is never empty.
>> This is redundant. In order to pick a random element, you’re saying I should have to do “Int.random(0 ..< 10)”? The redundancy here is that I have to specify Int twice: once for the “.random” call, and again for the type of the range. We can do better than that.
> I don’t see how this is redundant—do you mean that you’d need to write out the type name instead of writing `(0..<10).random()!`? I continue to be of the opinion that picking a random integer/floating-point value/Boolean and picking a random element from a collection are two different operations. Yes, there’s overlap between the two, but they’re different enough that having two ways to choose an integer between 0 and 10 would be fine.
> As I wrote elsewhere, if they're sufficiently different enough, then they ought to have distinct names. If they are not, then there ought to be only one of them. I'm no longer sure myself of which one I'd prefer, but in no case should there be two things named "random."
> If the proposal to make `IndexDistance == Int` is accepted, then ranges cannot conform to Collection and we encounter some interesting difficulties attempting to index into such a collection to pick a random value.
I'd like to put forth that Gameplay Kit offers a perfectly cromulent model of random number generation and API. Why not take that as a starting point, Swiftify it so it becomes a cross platform solution, and then apply the specific questions of sequences, collections, and indices as a second step?
I don't support intermingling random functionality with existing core types.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution