How Many Visitors?

Python Puzzles

Back to the Python! homepage


If my super fancy puzzle website averages 12 visitors a day, what are the chances of the following?

Getting exactly 10 visitors a day?

Getting 10 or more visitors a day?

You will need to use a Poisson distribution to solve this problem.


The probability of getting exactly 10 visitors a day is 10%
The probability of getting 10 or more visitors a day is 75%

The code is pretty simple using Numpy.

size = 1_000_000
lam = 12
 
import numpy as np

# generate a Poisson distribution with lambda=12 and size=1 million
np_poisson = np.random.poisson(lam=lam, size=size)
 
# calculate the probability of getting exactly 10 in a Poisson distribution
filter = np_poisson == 10
new_arr = np_poisson[filter]
 
print(np.size(new_arr)/size)
 
# calculate the probability of getting exactly 10 or more in a Poisson distribution
filter = np_poisson >= 10
new_arr = np_poisson[filter]
 
print(np.size(new_arr)/size)