[swift-users] Incorrect Fisher-Yates shuffle in example code

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Sat Dec 16 20:19:54 CST 2017


On Sat, Dec 16, 2017 at 7:37 PM, Daniel Dunbar <daniel_dunbar at apple.com>
 wrote:

> Would you like to post a PR to fix these issues?
>
>  - Daniel
>

All right, I’ve submitted a PR that I think should work, though I’m not too
confident in the pre-Swift-4 parts (credit to swiftdoc.org if the code for
old versions is valid).


On Sat, Dec 16, 2017 at 8:03 PM, Saagar Jha <saagar at saagarjha.com> wrote:

>
> Both of the original guard statements would be superfluous here (notably,
> “swapAt” is documented to have no effect when i and j are the same) so I
> removed them.
>
>
> Actually, I believe the first guard is still necessary–if the collection
> is empty, you’d end up with trying to construct the range 0..<-1, which
> traps. Alternatively, stride(from:to) is more lenient.
>

Right you are, not sure how I missed that. Muphry’s law
<https://en.wikipedia.org/wiki/Muphry%27s_law> strikes again!

Nevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171216/e4221f78/attachment.html>


More information about the swift-users mailing list