diff --git a/stdlib/public/core/FloatingPoint.swift.gyb b/stdlib/public/core/FloatingPoint.swift.gyb index bd5adf17d5d91..ab6d82958a5d5 100644 --- a/stdlib/public/core/FloatingPoint.swift.gyb +++ b/stdlib/public/core/FloatingPoint.swift.gyb @@ -2441,23 +2441,23 @@ where Self.RawSignificand : FixedWidthInteger { delta.isFinite, "There is no uniform distribution on an infinite range" ) - let rand: Self.RawSignificand - if Self.RawSignificand.bitWidth == Self.significandBitCount + 1 { - rand = generator.next() + let rand: RawSignificand + if RawSignificand.bitWidth == Self.significandBitCount + 1 { + rand = RawSignificand._random(using: &generator) % if 'Closed' in Range: - let tmp: UInt8 = generator.next() & 1 - if rand == Self.RawSignificand.max && tmp == 1 { + let tmp = UInt8._random(using: &generator) & 1 + if rand == RawSignificand.max && tmp == 1 { return range.upperBound } % end } else { let significandCount = Self.significandBitCount + 1 - let maxSignificand: Self.RawSignificand = 1 << significandCount + let maxSignificand: RawSignificand = 1 << significandCount % if 'Closed' not in Range: // Rather than use .next(upperBound:), which has to work with arbitrary // upper bounds, and therefore does extra work to avoid bias, we can take // a shortcut because we know that maxSignificand is a power of two. - rand = generator.next() & (maxSignificand - 1) + rand = RawSignificand._random(using: &generator) & (maxSignificand - 1) % else: rand = generator.next(upperBound: maxSignificand + 1) if rand == maxSignificand { diff --git a/stdlib/public/core/Integers.swift b/stdlib/public/core/Integers.swift index b84f3b90d1c5c..63ee2aee47433 100644 --- a/stdlib/public/core/Integers.swift +++ b/stdlib/public/core/Integers.swift @@ -2621,7 +2621,7 @@ extension FixedWidthInteger { // If we used &+ instead, the result would be zero, which isn't helpful, // so we actually need to handle this case separately. if delta == Magnitude.max { - return Self(truncatingIfNeeded: generator.next() as Magnitude) + return Self(truncatingIfNeeded: Magnitude._random(using: &generator)) } // Need to widen delta to account for the right-endpoint of a closed range. delta += 1 diff --git a/stdlib/public/core/Random.swift b/stdlib/public/core/Random.swift index 8dbb2d4f507a1..97f8b57b95874 100644 --- a/stdlib/public/core/Random.swift +++ b/stdlib/public/core/Random.swift @@ -67,38 +67,6 @@ public protocol RandomNumberGenerator { } extension RandomNumberGenerator { - @inlinable - public mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer) { - // FIXME: Optimize - var chunk: UInt64 = 0 - var chunkBytes = 0 - for i in 0..>= UInt8.bitWidth - chunkBytes -= 1 - } - } -} - -extension RandomNumberGenerator { - /// Returns a value from a uniform, independent distribution of binary data. - /// - /// Use this method when you need random binary data to generate another - /// value. If you need an integer value within a specific range, use the - /// static `random(in:using:)` method on that integer type instead of this - /// method. - /// - /// - Returns: A random value of `T`. Bits are randomly distributed so that - /// every value of `T` is equally likely to be returned. - @inlinable - public mutating func next() -> T { - return T._random(using: &self) - } - /// Returns a random value that is less than the given upper bound. /// /// Use this method when you need random binary data to generate another @@ -111,7 +79,7 @@ extension RandomNumberGenerator { /// - Returns: A random value of `T` in the range `0..( + internal mutating func next( upperBound: T ) -> T { _precondition(upperBound != 0, "upperBound cannot be zero.") @@ -120,11 +88,27 @@ extension RandomNumberGenerator { var random: T = 0 repeat { - random = next() + random = T._random(using: &self) } while random < range return random % upperBound } + + @inlinable + public mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer) { + // FIXME: Optimize + var chunk: UInt64 = 0 + var chunkBytes = 0 + for i in 0..>= UInt8.bitWidth + chunkBytes -= 1 + } + } } /// The system's default source of random data.