Merge pull request #20320 from Azoy/random-readability

[QoI][stdlib] Improve some random call sites
This commit is contained in:
Harlan Haskins
2018-11-09 11:31:43 -08:00
committed by GitHub
2 changed files with 5 additions and 9 deletions

View File

@@ -452,16 +452,15 @@ extension MutableCollection where Self : RandomAccessCollection {
public mutating func shuffle<T: RandomNumberGenerator>(
using generator: inout T
) {
let count = self.count
guard count > 1 else { return }
var amount = count
var currentIndex = startIndex
while amount > 1 {
let random = generator.next(upperBound: UInt(amount))
let random = Int.random(in: 0 ..< amount, using: &generator)
amount -= 1
swapAt(
currentIndex,
index(currentIndex, offsetBy: numericCast(random))
index(currentIndex, offsetBy: random)
)
formIndex(after: &currentIndex)
}