Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (system-wide)
import random as r from numba import njit import numpy as np import matplotlib.pyplot as plt
# returns how many pours it took to fill the cup @njit def fill_the_cup(): cup = 0 pours = 0 while cup < 1: cup += r.uniform(0,1) pours += 1 return pours
runs = 10_000_000 results = np.zeros(15) for k in range(runs): results[fill_the_cup()] += 1
plt.ylabel('Frequency'); plt.xlabel('pours'); plt.bar(range(len(results)), results, 1/1.5, color="blue"); plt.show()
Image in a Jupyter notebook

necessary pours=k[k×(proportion of time need k pours)]\overline{\text{necessary pours}}=\sum_k [k \times (\text{proportion of time need k pours})]

(np.array(range(15)) @ results)/runs
2.7184591