Search code examples
pythonnumpyscipycoin-flipping

solution for the flipping coin issue


I am trying to solve this prolem : a random experiment of tossing a coin 10000 times and determine the count of Heads:: defining a binomial distribution with n = 1 and p = 0.5. using binom function from scipy.stats setting random seed to 1 Draw a sample of 10000 elements from defined distribution. Assume the values 0 and 1 represent Heads and Tails respectively.

Counting the number of heads and display it by using 'bincount' method,

the code snippet is :

import scipy as sp
from scipy import stats
import numpy as np

n, p = 1, .5  # number of trials, probability of each trial
s = np.random.binomial(n, p, 1000)
print(s)
k = np.bincount(s)


print(k[0])
print(k[1])
print(k.count(1))

Where it is going wrong? I need to find out the number of heads from bincount()


Solution

  • If you want 10000 trials, then change n, p = 1, .5 to n, p = 10000, .5