Proposed amendment to SE-202: Replace `Collection.randomElement` requirement with `randomIndex`

That’s true, but most things you would do with such a distance sound unreasonable anyway—if you’re preallocating a buffer larger than Int.max, or preparing to compare two Collections larger than Int.max, the operation you want to perform is unreasonable and the crash when you try to fetch the count saves you from a much slower, more expensive failure. That isn’t true for randomElement()—it’s eminently reasonable to write, say, func randomElements(_ n: Int) -> Set<Element> in terms of randomElement(), eminently reasonable to use it on a big range, and eminently reasonable to feel very angry that the standard library fails you.

3 Likes