Skip to content

[SR-8178] Fix BinaryFloatingPoint.random(in:) open range returning upperBound #17794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stdlib/public/core/FloatingPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,13 @@ where Self.RawSignificand : FixedWidthInteger {
% end
}
let unitRandom = Self.init(rand) * Self.ulpOfOne / 2
return delta * unitRandom + range.lowerBound
let randFloat = delta * unitRandom + range.lowerBound
% if 'Closed' not in Range:
if randFloat == range.upperBound {
return Self.random(in: range, using: &generator)
}
% end
return randFloat
}

/// Returns a random value within the specified range.
Expand Down
61 changes: 54 additions & 7 deletions validation-test/stdlib/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ RandomTests.test("basic random numbers") {

// Random integers in ranges

func integerRangeTest<T: FixedWidthInteger>(_ type: T.Type)
where T.Stride: SignedInteger, T.Magnitude: UnsignedInteger {
func integerRangeTest<T: FixedWidthInteger>(_ type: T.Type) {

func testRange(_ range: Range<T>, iterations: Int = 1_000) {
var integerSet: Set<T> = []
Expand Down Expand Up @@ -104,10 +103,8 @@ RandomTests.test("random integers in ranges") {
// Random floating points in ranges

func floatingPointRangeTest<T: BinaryFloatingPoint>(_ type: T.Type)
where T.RawSignificand: FixedWidthInteger,
T.RawSignificand.Stride: SignedInteger & FixedWidthInteger,
T.RawSignificand.Magnitude: UnsignedInteger {

where T.RawSignificand: FixedWidthInteger {

let testRange = 0 ..< 1_000

// open range
Expand Down Expand Up @@ -230,6 +227,56 @@ RandomTests.test("different random number generators") {
expectEqual(shufflePasses[0], shufflePasses[1])
}

// Random floating points with max values (SR-8178)

var lcrng = LCRNG(seed: 1234567890)

public struct RotatingRNG: RandomNumberGenerator {
public let rotation: [() -> UInt64] = [
{ return .min },
{ return .max },
{ return lcrng.next() }
]
public var rotationIndex = 0

public mutating func next() -> UInt64 {
if rotationIndex == rotation.count {
rotationIndex = 0
}

defer {
rotationIndex += 1
}

return rotation[rotationIndex]()
}
}

func floatingPointRangeMaxTest<T: BinaryFloatingPoint>(_ type: T.Type)
where T.RawSignificand: FixedWidthInteger {

let testRange = 0 ..< 1_000

var rng = RotatingRNG()
let ranges: [Range<T>] = [0 ..< 1, 1 ..< 2, 10 ..< 11, 0 ..< 10]
for range in ranges {
for _ in testRange {
let random = T.random(in: range, using: &rng)
expectTrue(range.contains(random))
expectTrue(range.lowerBound <= random)
expectTrue(random < range.upperBound)
}
}
}

RandomTests.test("random floating point range maxes") {
floatingPointRangeMaxTest(Float.self)
floatingPointRangeMaxTest(Double.self)
#if !os(Windows) && (arch(i386) || arch(x86_64))
floatingPointRangeMaxTest(Float80.self)
#endif
}

// Uniform Distribution

func chi2Test(_ samples: [Double]) -> Bool {
Expand All @@ -248,7 +295,7 @@ func chi2Test(_ samples: [Double]) -> Bool {

if chi2 > cvHigh {
return false
}else {
} else {
return true
}
}
Expand Down