Skip to content

Commit 7a61fef

Browse files
committed
Refactor {Closed}Range.random
Fix FloatingPoint initialization Precondition getting a random number from range
1 parent 410e2f7 commit 7a61fef

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ extension BinaryFloatingPoint {
21122112
let bits = MemoryLayout<Self>.size * 8
21132113
let fraction = bits - Self.exponentBitCount
21142114
let random = generator.next(UInt64.self) >> (bits - fraction)
2115-
return Self(random) / Self(1 << fraction)
2115+
return Self.init(random) / Self.init(1 << fraction)
21162116
}
21172117

21182118
/* TODO: uncomment these default implementations when it becomes possible

stdlib/public/core/Random.swift.gyb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ extension Randomizable where Self: BinaryFloatingPoint {
199199
/// Shorthand for using the random(in:using:) function. This uses the default
200200
/// random implementation defined in the standard library.
201201
public static func random(in range: ${Range}<Self>) -> Self {
202+
_precondition(
203+
!range.isEmpty,
204+
"Range should not be empty when getting a random number"
205+
)
202206
return range.random!
203207
}
204208

@@ -212,6 +216,10 @@ extension Randomizable where Self: BinaryFloatingPoint {
212216
in range: ${Range}<Self>,
213217
using generator: T
214218
) -> Self {
219+
_precondition(
220+
!range.isEmpty,
221+
"Range should not be empty when getting a random number"
222+
)
215223
return range.random(using: generator)!
216224
}
217225
}
@@ -229,6 +237,10 @@ extension Randomizable where Self: Strideable, Self.Stride: SignedInteger {
229237
/// Shorthand for using the random(in:using:) function. This uses the default
230238
/// random implementation defined in the standard library.
231239
public static func random(in range: ${Range}<Self>) -> Self {
240+
_precondition(
241+
!range.isEmpty,
242+
"Range should not be empty when getting a random number"
243+
)
232244
return range.random!
233245
}
234246

@@ -242,6 +254,10 @@ extension Randomizable where Self: Strideable, Self.Stride: SignedInteger {
242254
in range: ${Range}<Self>,
243255
using generator: T
244256
) -> Self {
257+
_precondition(
258+
!range.isEmpty,
259+
"Range should not be empty when getting a random number"
260+
)
245261
return range.random(using: generator)!
246262
}
247263
}

stdlib/public/core/Range.swift.gyb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,11 @@ extension ${Self} where Bound: BinaryFloatingPoint {
763763
public func random<T: RandomNumberGenerator>(using generator: T) -> Bound? {
764764
guard !isEmpty else { return nil }
765765
% if 'Closed' not in Self:
766-
var random: Bound? =
767-
Bound.random(using: generator) * abs(lowerBound - upperBound) + lowerBound
768-
if random == upperBound { random = self.random(using: generator) }
769-
% else:
770766
let random =
771767
Bound.random(using: generator) * abs(lowerBound - upperBound) + lowerBound
768+
% else:
769+
var random = Bound(generator.next(UInt64.self)) / Bound(UInt64.max)
770+
random = random * abs(lowerBound - upperBound) + lowerBound
772771
% end
773772
return random
774773
}

0 commit comments

Comments
 (0)