# Biased Coin Flipping simulation in Python

In this tutorial, we will learn to simulate the occurrence of a biased coin in Python programming. This is great way to make an inference from the data using the concepts of probability. Here we have used Numpy and Matplotlib libraries to simulate the biased coin flip experiment with Python.

```import numpy as np
import matplotlib.pyplot as plt```

In a biased coin, the probability of getting head or tail is unequal. As a result, the probability of occurrence can be anything other than 0.5. For example, if the coin produces more heads than tails, it’s probability will be more than 0.5 else it will be less than 0.5.

## Flipping a Biased Coin

Look at our Python code below:

```Trials = 1
prob_ty = 0.8
Outcome = np.random.binomial(Trials,prob_ty)
print(Outcome)```

Output:

`1`

If we run the above code, there is an eighty percent chance of getting 1 as the output and twenty percent chance of getting 0 as output. We can easily repeat this task multiple times by increasing the value of ‘Trials’. Rather think of flipping multiple coins simultaneously. Here we have used probability. Most importantly Numpy binomial distribution, np.random.binomial(Trials,prob_ty).

#### Syntax : numpy.random.binomial(n,p,size)

• n -> number of trials in an experiment.
• p -> probability of success/Heads.
• size -> number of experiments.

## Repetition of Biased Coin Experiment

Let us increase the Trials by 10 times also the experiment by 50 times. Hence we can know the code reliability. The output will be the number of heads in each of the experiments.

```def event(n,prob_ty,size):
Outcome = np.random.binomial(n,prob_ty,size)
return Outcome

Prob_ty = 0.8
Trials = 10
N_expts = 50

Outcome = event(Trials,Prob_ty,N_expts)

print("Number of Heads in each Experiment:\n",Outcome)```
```Output:
Number of Heads in each Experiment:
[ 8  8  8  5  7  10  7  8  7  8  8  9  6  9  9  8  9  8  9  9  7  8  8  7  9  5  8  7  8  8  8  8  7  8  7  8  8  9  8 10  6 10  8 10  9  8  8  7  8  8]
```

Finally, we can plot the data using the Matplotlib library. We will set the probability of Heads = 0.8. As a result, we should observe that the probability of success is more when the count of Heads is 8.

```Prob_expt=[np.equal(Outcome,i).mean() for i in range(Trials)]

plt.plot(Prob_expt,'r--')
plt.xlabel('Number of Heads in each Experiment')
plt.ylabel('Probability of Occurrence',)
plt.show()
``` ### Here is how the complete code should look like

```import numpy as np
import random
import matplotlib.pyplot as plt

def event(n,prob_ty,size):
Outcome = np.random.binomial(n,prob_ty,size)
return Outcome

Prob_ty = 0.8
Trials = 10
N_expts = 50

Outcome = event(Trials,Prob_ty,N_expts)
print("Number of Heads in each Experiment:\n",Outcome)

Prob_expt=[np.equal(Outcome,i).mean() for i in range(Trials)]

plt.plot(Prob_expt,'r--')
plt.xlabel('Number of Heads in each Experiment')
plt.ylabel('Probability of Occurrence',)
plt.show()```

Note: For an Unbiased coin experiment we just need to set the probability of success as 0.5.

I hope you would be benefited from this tutorial..!