NumPy Binomial Distribution
A binomial draw counts successes in a fixed number of trials with a specified success probability. It returns an integer count, not a percentage.
How it works
The usual model assumes independent trials with the same probability. A toy inspection experiment might count how many of eight identical sample containers pass a fictional check. Real observations may share conditions or have different probabilities, which would violate that simple interpretation. Keep the parameter n separate from the number of simulated experiments requested through size.
Run the code
Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.
For local work, use a separate virtual environment and run python binomial.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.
Example
import numpy as np
draws = np.random.default_rng(41).binomial(n=8, p=0.75, size=50)
print(draws.shape)
print(bool(((draws >= 0) & (draws <= 8)).all()))
print(np.random.default_rng(41).binomial(n=8, p=1., size=3).tolist())
Expected result
(50,) True [8, 8, 8]
Fifty simulated experiments each contain eight possible successes. The p=1 boundary produces the full count every time and gives a stronger deterministic check than expecting one ordinary random sample to equal its theoretical average.
Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.