Home »
Python »
Python Programs
Python numpy.random.binomial() Method with Example
Python | numpy.random.binomial() Method: In this tutorial, we will learn about the numpy.random.binomial() method with its usages, syntax, parameters, return type, and examples.
By Pranit Sharma Last updated : December 25, 2023
What is Binomial Distribution?
In mathematics, the binomial distribution with parameters n and p is the discrete probability distribution in a sequence of n independent experiments which gives only two possible results either success or failure.
What is numpy.random.binomial() Method?
The numpy.random.binomial() method is used to draw samples from a binomial distribution. Where, the samples are drawn from a binomial distribution with specified parameters, n trials, and p probability of success where n is an integer >= 0 and p is in the interval [0,1].
Syntax
random.binomial(n, p, size=None)
Parameter(s)
- n: parameters of the distribution which are greater than or equal to 0.
- p: parameters of the distribution which are greater than or equal to 0 and less than or equal to 1.
- size: shape of the output.
Let us understand with the help of an example,
numpy.random.binomial() Method Example 1
# Import numpy
import numpy as np
# Defining number of trial
n = 10
# Define possibility
p = 0.5
# Draw binomial distribution
res = np.random.binomial(n,p,100)
# Display result
print("Result:\n",res,"\n")
Output
Result:
[6 7 5 8 7 6 6 5 7 7 4 5 3 5 6 4 8 4 4 5 4 2 8 3 6 3 6 4 7 5 2 5 5 4 6 5 4
3 5 1 3 6 6 4 3 4 6 5 6 7 4 5 5 2 2 7 6 3 2 4 5 6 5 8 5 0 3 6 3 4 3 6 2 5
1 5 7 3 6 3 2 4 7 7 4 4 5 6 4 5 3 2 6 4 5 5 5 2 7 6]
numpy.random.binomial() Method Example 2
from numpy import random
x = random.binomial(n=100, p=0.5, size=10)
print(x)
Output
[43 52 47 62 48 44 44 54 62 47]
Reference: numpy.random.binomial
Python NumPy Programs »