File tree Expand file tree Collapse file tree 2 files changed +5
-9
lines changed Expand file tree Collapse file tree 2 files changed +5
-9
lines changed Original file line number Diff line number Diff line change @@ -929,12 +929,9 @@ extension Collection {
929
929
using generator: inout T
930
930
) -> Element ? {
931
931
guard !isEmpty else { return nil }
932
- let random = generator. next ( upperBound: UInt ( count) )
933
- let index = self . index (
934
- startIndex,
935
- offsetBy: numericCast ( random)
936
- )
937
- return self [ index]
932
+ let random = Int . random ( in: 0 ..< count, using: & generator)
933
+ let idx = index ( startIndex, offsetBy: random)
934
+ return self [ idx]
938
935
}
939
936
940
937
/// Returns a random element of the collection.
Original file line number Diff line number Diff line change @@ -452,16 +452,15 @@ extension MutableCollection where Self : RandomAccessCollection {
452
452
public mutating func shuffle< T: RandomNumberGenerator > (
453
453
using generator: inout T
454
454
) {
455
- let count = self . count
456
455
guard count > 1 else { return }
457
456
var amount = count
458
457
var currentIndex = startIndex
459
458
while amount > 1 {
460
- let random = generator . next ( upperBound : UInt ( amount) )
459
+ let random = Int . random ( in : 0 ..< amount, using : & generator )
461
460
amount -= 1
462
461
swapAt (
463
462
currentIndex,
464
- index ( currentIndex, offsetBy: numericCast ( random) )
463
+ index ( currentIndex, offsetBy: random)
465
464
)
466
465
formIndex ( after: & currentIndex)
467
466
}
You can’t perform that action at this time.
0 commit comments