Skip to content

Commit e4719f8

Browse files
committed
Make FloatingPoint.random exclusive
1 parent 7326b68 commit e4719f8

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,11 +2106,13 @@ extension BinaryFloatingPoint {
21062106
/// - Returns: A random representation of this floating point conforming to
21072107
/// `Randomizable`
21082108
///
2109-
/// The range of this random floating point is from 0 to 1 inclusive
2109+
/// The range of this random floating point is from 0 to 1 exclusive
21102110
@_inlineable
21112111
public static func random<T: RandomNumberGenerator>(using generator: T) -> Self {
2112-
let random = generator.next(UInt64.self)
2113-
return Self(random) / Self(UInt64.max)
2112+
let bits = MemoryLayout<Self>.size * 8
2113+
let fraction = bits - Self.exponentBitCount
2114+
let random = generator.next(UInt64.self) >> (bits - fraction)
2115+
return Self(random) / Self(1 << fraction)
21142116
}
21152117

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

0 commit comments

Comments
 (0)