Generate Binomially Distributed Data in Python

Your goal

You need to randomly generate binomially distributed data in Python.

Step-by-step tutorial

We will use the binomial random number generator from numpy.random.default_rng.

>>> from numpy.random import default_rng
>>> rng = default_rng()
>>> data = rng.binomial(30, 0.4, 5000)
>>> data
array([11, 10,  9, ..., 11, 15, 12])

Here's a histogram for the data:

>>> plt.hist(data, bins=range(30))
Histogram of binomially distributed data
Histogram of binomially distributed data