TheAlgorithms/Python · error · ValueError
prob has to be in range of 1 - 0
Error message
prob has to be in range of 1 - 0
What it means
The final guard in binomial_distribution requires 0 < prob < 1 strictly; a probability of exactly 0, exactly 1, or anything outside raises ValueError('prob has to be in range of 1 - 0'). The implementation's probability formula assumes an interior probability.
Source
Thrown at maths/binomial_distribution.py:27
Return probability of k successes out of n tries, with p probability for one
success
The function uses the factorial function in order to calculate the binomial
coefficient
>>> binomial_distribution(3, 5, 0.7)
0.30870000000000003
>>> binomial_distribution (2, 4, 0.5)
0.375
"""
if successes > trials:
raise ValueError("""successes must be lower or equal to trials""")
if trials < 0 or successes < 0:
raise ValueError("the function is defined for non-negative integers")
if not isinstance(successes, int) or not isinstance(trials, int):
raise ValueError("the function is defined for non-negative integers")
if not 0 < prob < 1:
raise ValueError("prob has to be in range of 1 - 0")
probability = (prob**successes) * ((1 - prob) ** (trials - successes))
# Calculate the binomial coefficient: n! / k!(n-k)!
coefficient = float(factorial(trials))
coefficient /= factorial(successes) * factorial(trials - successes)
return probability * coefficient
if __name__ == "__main__":
from doctest import testmod
testmod()
print("Probability of 2 successes out of 4 trails")
print("with probability of 0.75 is:", end=" ")
print(binomial_distribution(2, 4, 0.75))
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a strict interior probability (e.g. 1e-9 < p < 1 - 1e-9).
- Laplace-smooth estimated probabilities: p = (successes + 1) / (trials + 2).
- Divide percentages by 100 before calling.
Example fix
# before p_x = binomial_distribution(k, n, 70) # percent, not probability # after p = 70 / 100 p_x = binomial_distribution(k, n, p)
Defensive patterns
Strategy: validation
Validate before calling
if not 0 < prob < 1:
raise ValueError(f"prob must be strictly between 0 and 1, got {prob}")
p = binomial_distribution(successes, trials, prob) Type guard
def is_interior_probability(p: object) -> bool:
return isinstance(p, (int, float)) and 0 < p < 1 Prevention
- Endpoints 0.0 and 1.0 are rejected — clamp with a small epsilon if needed
- Laplace-smooth degenerate estimates: p = (k + 1) / (n + 2)
- Convert percentages (e.g. 70) to fractions (0.7) before calling
When it happens
Trigger: binomial_distribution(2, 4, 1.0); binomial_distribution(2, 4, 0); binomial_distribution(2, 4, 1.5); prob computed as 1 - epsilon that rounds to exactly 1.0.
Common situations: Edge-case probabilities from degenerate data (all successes observed -> p estimated as 1.0); clamping code that snaps to [0,1] inclusive; user-entered percentages like 70 passed instead of 0.7.
Related errors
- successes must be lower or equal to trials
- the function is defined for non-negative integers
- Limit for the Catalan sequence must be ≥ 0
- Number should not be negative.
- Negative arguments are not supported
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/89354fd329bb30b0.
Report an issue: GitHub.