• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Numpy Tips / How to generate random numbers from binomial distribution in Numpy

How to generate random numbers from binomial distribution in Numpy

December 30, 2022 by cmdlinetips

In this post we will learn the basics of generating random numbers from binomial distribution in Python with Numpy. We will use Numpy’s random module’s binomial() function to generatee random numbers with multiple examples.

What is Binomial Distribution

The binomial distribution is a probability distribution that describes the outcomes of a series of independent trials with two outcomes, like Head or Tail and Success or Failure. It is useful modeling processes where. there are fixed. number of independent trials with fixed probability of success.

The binomial distribution is defined by two parameters: the number of trials, n, and the probability of success on a single trial, p. If the probability of success on a single trial is p, then the probability of failure on a single trial is 1 – p.

The probability of the number of successes, x, in the series of n trials in a binomial distribution can be computed using the formula below


P(x) = (n choose x) * p^x * (1-p)^(n-x)

where (n choose x) is called the binomial coefficient, which is calculated as:

(n choose x) = n! / (x! * (n-x)!)

The binomial coefficient represents the number of ways in which x successes can occur in n trials.

The binomial distribution is a discrete distribution. By that we mean the possible values for x are integers from 0 to n.

Generating a single Random Number from Binomial distribution with NumPy Python

The numpy.random.binomial() function takes two parameters: the number of trials and the probability of success for each trial. It returns a random integer from the binomial distribution with the specified number of trials and probability of success.

import numpy as np

We will use Numpy’s Random Generator class by instantiating default_rng() function from random module to generate random numbers.

rng = np.random.default_rng(2022)

Let us generate a random number from a binomial distribution with 10 trials and a probability of success of 0.5. Numpy’s binomial() function can take n, the number of trials and p, the probability of success to generate a random number from binomial distribution.

rng.binomial(n=10, p=0.5)

The output could be any integer between 0 and 10, inclusive, depending on the outcome of the 10 Bernoulli trials.

5

We can also generate a random number from the binomial distribution with 10 trials and a probability of success of 0.5 without specifying the argument names.

rng.binomial(10, 0.5)
3

Generating Random Numbers from Binomial distribution with NumPy Python

Here is another example of how to use the Numpy’s binomial() function using Random Generator Class. And this time we generate multiple random numbers.

In the example below, we generate 5 random numbers from a binomial distribution with n=10 trials and a probability of success of p=0.5:

rng.binomial(n=10, p=0.5, size=5)

array([3, 6, 6, 3, 2])

Wee can also generate random numbers from a biased coin by changing the probability of success p. Below, we generate an array of 5 random numbers from the binomial distribution with 10 trials and a probability of success of 0.8.

The output will be an array of 5 integers, each of which could be any integer between 0 and 10, inclusive, depending on the outcome of the 10 Bernoulli trials.

rng.binomial(n=10, p=0.8, size=5)

array([5, 9, 8, 9, 6])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to generate random numbers from Poisson distribution with Numpy Default ThumbnailHow to generate random numbers from Normal distribution in Numpy Default ThumbnailGenerate Random Integers using Generators in Numpy Generate Random Numbers from Normal Distribution in RHow To Generate Random Numbers from Probability Distributions in R?

Filed Under: Numpy Tips Tagged With: Numpy binom()

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version