I am asking on how the algorithm works to produce the numbers. – WhiteSolstice 35 mins ago
Non-technical explanation
If you pass n=1
to the Binomial distribution it is equivalent to the Bernoulli distribution. In this case the function could be thought of simulating coin flips. size=3
tells it to flip the coin three times and p=0.5
makes it a fair coin with equal probabilitiy of head (1) or tail (0).
The result of [1 0 0]
means the coin came down once with head and twice with tail facing up. This is random, so running it again would result in a different sequence like [1 1 0]
, [0 1 0]
, or maybe even [1 1 1]
. Although you cannot get the same number of 1s and 0s in three runs, on average you would get the same number.
Technical explanation
Numpy implements random number generation in C. The source code for the Binomial distribution can be found here. Actually two different algorithms are implemented.
- If
n * p <= 30
it uses inverse transform sampling. - If
n * p > 30
the BTPE algorithm of (Kachitvichyanukul and Schmeiser 1988) is used. (The publication is not freely available.)
I think both methods, but certainly the inverse transform sampling, depend on a random number generator to produce uniformly distributed random numbers. Numpy internally uses a Mersenne Twister pseudo random number generator. The uniform random numbers are then transformed into the desired distribution.