Skip to content
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
9 changes: 8 additions & 1 deletion Sources/Random/Binomial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ import RealModule
}
}
extension Binomial {
@inlinable public static subscript(n: Int64, p: Double) -> Self { .init(n: n, p: p) }
@inlinable public static subscript(n: Int64, p: Double) -> Self {
guard p.isFinite else {
fatalError("Binomial.subscript cannot be called with `p` = \(p)")
}
return .init(n: n, p: p)
}
Comment on lines +20 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If n is negative, calling pdf(_:) will attempt to construct an invalid range 0 ... self.n, which triggers a runtime fatal error in Swift (Can't form Range with upperBound < lowerBound). Guarding that n >= 0 in the subscript prevents this crash and ensures the distribution is always initialized in a valid state.

Suggested change
@inlinable public static subscript(n: Int64, p: Double) -> Self {
guard p.isFinite else {
fatalError("Binomial.subscript cannot be called with `p` = \(p)")
}
return .init(n: n, p: p)
}
@inlinable public static subscript(n: Int64, p: Double) -> Self {
guard n >= 0 else {
fatalError("Binomial.subscript cannot be called with negative `n` = \(n)")
}
guard p.isFinite else {
fatalError("Binomial.subscript cannot be called with `p` = \(p)")
}
return .init(n: n, p: p)
}

}
extension Binomial {
/// Sample the binomial distribution. As long as ``n`` is non-negative, the result is
/// guaranteed to lie in the range `0 ... n`.
@inlinable public func sample(using generator: inout some RandomNumberGenerator) -> Int64 {
if self.p <= 0 { return 0 }
if self.p >= 1 { return self.n }
Expand Down
20 changes: 20 additions & 0 deletions Sources/RandomTests/Distributions/BinomialGuarantees.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Random
import Testing

@Suite struct BinomialGuarantees {
private var random: PseudoRandom

init() {
self.random = .init(seed: 10)
}

@Test mutating func OutputRange() {
let n: Int64 = Int64.max - 1
let distribution = Binomial[n, 1.nextDown]
for _: Int in 0 ..< 1000 {
let result: Int64 = distribution.sample(using: &self.random.generator)
#expect(result >= 0)
#expect(result <= n)
}
}
Comment on lines +11 to +19

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To thoroughly test the binomial guarantees and boundary conditions, consider adding test cases for edge cases such as n = 0, p = 0, and p = 1.

    @Test mutating func OutputRange() {
        let n: Int64 = Int64.max - 1
        let distribution = Binomial[n, 1.nextDown]
        for _: Int in 0 ..< 1000 {
            let result: Int64 = distribution.sample(using: &self.random.generator)
            #expect(result >= 0)
            #expect(result <= n)
        }

        // Test boundary edge cases
        #expect(Binomial[0, 0.5].sample(using: &self.random.generator) == 0)
        #expect(Binomial[10, 0.0].sample(using: &self.random.generator) == 0)
        #expect(Binomial[10, 1.0].sample(using: &self.random.generator) == 10)
    }

}