[swift-evolution] [Proposal] Random Unification
Jonathan Hull
jhull at gbis.com
Thu Nov 30 15:52:51 CST 2017
For collections, I think we should call returning a random element -randomElement, and choosing a random element without replacement -popRandomElement
var list = [1,2,3,4]
let a:Int? = list.randomElement //List is still [1,2,3,4] and ‘a’ contains one of the elements
let b:Int? = list.popRandomElement //Now list contains all the elements except the one in ‘b’
Thanks,
Jon
> On Nov 30, 2017, at 10:15 AM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>
> Right—again, this is yet another difference between two things both named “random” in this proposal. Almost always, when we say “give me two random integers,” we want independent random samples from the set of all integers. But for collections, a very common (and probably more common operation) is to _sample without replacement_. Conflating the two is how hard-to-detect errors arise.
>
> On Thu, Nov 30, 2017 at 10:29 Martin Waitz via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Hi Erica,
>
> > Doesn't `choose` usually take two arguments, the `count` to choose (presumably defaulting to 1) and the collection to choose `from`?
>
> This might be useful for collections, when you want to draw several elements without drawing the same element twice.
> For ranges of random numbers you usually need independent numbers and just call the generator multiple times.
> Both use cases could be provided as overloads (a default argument would require that the return value has the same type, which would be unfortunate here).
>
> --
> Martin
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171130/8e8a8ed9/attachment.html>
More information about the swift-evolution
mailing list