The random.binomialvariate() method in Python used to generates a random number that follows a binomial distribution. This method returns the number of successes in n independent trials, given that each trial has a probability p of success.
The equivalent mathematical expression of this random.binomialvariate() method is as follows −
sum(random() < p for i in range(n))
The number of trials n must be a non-negative integer, and p must be a probability value between 0 and 1 (inclusive). These ensure that the generated random variable conforms to the properties of a binomial distribution and the resultant integer must be in the range 0 <= X <= n.
This method was introduced in Python version 3.12. If your Python version is earlier than 3.12, and you are attempting to use this method, it will raise an AttributeError: module 'random' has no attribute 'binomialvariate'.Syntax
Following is the syntax of the random.binomialvariate() method −
random.binomialvariate(n=1, p=0.5)Parameters
This method accepts the following parameter −
n: This parameter represents the number of independent trials or experiments. It must be a non-negative integer.
p: This is the probability of success on each trial. It must be a value between 0.0 and 1.0, inclusive.
This method returns an integer that represents the number of successes in the n trials.
Example 1Following is a basic example of the random.binomialvariate() method −
import random # number of trials n = 10 # probability of success in each trial p = 0.5 # Generate a random value following a binomial distribution number_of_successes = random.binomialvariate(n, p) print("Number of successes: ",number_of_successes)
Following is the output −
Number of successes: 4
Note: The Output generated will be different for every execution as it returns a random item.
Example 2Following is an example to generate the random numbers from a binomial distribution.
import random ATOMS = 1000000 DECAY_PROB = 0.1 for i in range(10): print(random.binomialvariate(ATOMS, DECAY_PROB))
While executing the above code you will get the similar output like below −
99424 99757 99791 100213 99970 99557 100113 100077 100354 100256
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4