Skip to content

Commit 7af435c

Browse files
committed
clean up some random generator calls
1 parent 43f04ee commit 7af435c

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

stdlib/public/core/Collection.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,12 @@ extension Collection {
929929
using generator: inout T
930930
) -> Element? {
931931
guard !isEmpty else { return nil }
932-
let random = generator.next(upperBound: UInt(count))
933-
let index = self.index(
932+
let random = Int.random(in: 0 ..< count)
933+
let idx = index(
934934
startIndex,
935-
offsetBy: numericCast(random)
935+
offsetBy: random
936936
)
937-
return self[index]
937+
return self[idx]
938938
}
939939

940940
/// Returns a random element of the collection.

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,16 +445,15 @@ extension MutableCollection where Self : RandomAccessCollection {
445445
public mutating func shuffle<T: RandomNumberGenerator>(
446446
using generator: inout T
447447
) {
448-
let count = self.count
449448
guard count > 1 else { return }
450449
var amount = count
451450
var currentIndex = startIndex
452451
while amount > 1 {
453-
let random = generator.next(upperBound: UInt(amount))
452+
let random = Int.random(in: 0 ..< amount)
454453
amount -= 1
455454
swapAt(
456455
currentIndex,
457-
index(currentIndex, offsetBy: numericCast(random))
456+
index(currentIndex, offsetBy: random)
458457
)
459458
formIndex(after: &currentIndex)
460459
}

0 commit comments

Comments
 (0)