{"record":{"id":"89354fd329bb30b0","repo":"TheAlgorithms/Python","slug":"prob-has-to-be-in-range-of-1-0","errorCode":null,"errorMessage":"prob has to be in range of 1 - 0","messagePattern":"prob has to be in range of 1 - 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/binomial_distribution.py","lineNumber":27,"sourceCode":"    Return probability of k successes out of n tries, with p probability for one\r\n    success\r\n\r\n    The function uses the factorial function in order to calculate the binomial\r\n    coefficient\r\n\r\n    >>> binomial_distribution(3, 5, 0.7)\r\n    0.30870000000000003\r\n    >>> binomial_distribution (2, 4, 0.5)\r\n    0.375\r\n    \"\"\"\r\n    if successes > trials:\r\n        raise ValueError(\"\"\"successes must be lower or equal to trials\"\"\")\r\n    if trials < 0 or successes < 0:\r\n        raise ValueError(\"the function is defined for non-negative integers\")\r\n    if not isinstance(successes, int) or not isinstance(trials, int):\r\n        raise ValueError(\"the function is defined for non-negative integers\")\r\n    if not 0 < prob < 1:\r\n        raise ValueError(\"prob has to be in range of 1 - 0\")\r\n    probability = (prob**successes) * ((1 - prob) ** (trials - successes))\r\n    # Calculate the binomial coefficient: n! / k!(n-k)!\r\n    coefficient = float(factorial(trials))\r\n    coefficient /= factorial(successes) * factorial(trials - successes)\r\n    return probability * coefficient\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    from doctest import testmod\r\n\r\n    testmod()\r\n    print(\"Probability of 2 successes out of 4 trails\")\r\n    print(\"with probability of 0.75 is:\", end=\" \")\r\n    print(binomial_distribution(2, 4, 0.75))\r\n","sourceCodeStart":9,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/binomial_distribution.py#L9-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\np_x = binomial_distribution(k, n, 70)  # percent, not probability\n\n# after\np = 70 / 100\np_x = binomial_distribution(k, n, p)","handlingStrategy":"validation","validationCode":"if not 0 < prob < 1:\n    raise ValueError(f\"prob must be strictly between 0 and 1, got {prob}\")\np = binomial_distribution(successes, trials, prob)","typeGuard":"def is_interior_probability(p: object) -> bool:\n    return isinstance(p, (int, float)) and 0 < p < 1","tryCatchPattern":null,"preventionTips":["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"],"tags":["math","probability","statistics","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}