diff options
author | Raymond Hettinger <python@rcn.com> | 2016-12-04 19:00:34 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-12-04 19:00:34 (GMT) |
commit | 71c62e14aa27d73623427a0a626b1f20df309e43 (patch) | |
tree | 0bfa49fb97f18bc35b16a7425e8af8d5e0c0a184 /Doc/library/random.rst | |
parent | 223813111e4a20b901af74485a5bc5fe737b4d6d (diff) | |
download | cpython-71c62e14aa27d73623427a0a626b1f20df309e43.zip cpython-71c62e14aa27d73623427a0a626b1f20df309e43.tar.gz cpython-71c62e14aa27d73623427a0a626b1f20df309e43.tar.bz2 |
Neaten-up and extend the examples in the random module docs.
Diffstat (limited to 'Doc/library/random.rst')
-rw-r--r-- | Doc/library/random.rst | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/Doc/library/random.rst b/Doc/library/random.rst index d96cc3e..4f25157 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -364,25 +364,29 @@ Basic examples:: Simulations:: - # Six roulette wheel spins (weighted sampling with replacement) + >>> # Six roulette wheel spins (weighted sampling with replacement) >>> choices(['red', 'black', 'green'], [18, 18, 2], k=6) ['red', 'green', 'black', 'black', 'red', 'black'] - # Deal 20 cards without replacement from a deck of 52 playing cards - # and determine the proportion of cards with a ten-value (i.e. a ten, - # jack, queen, or king). + >>> # Deal 20 cards without replacement from a deck of 52 playing cards + >>> # and determine the proportion of cards with a ten-value + >>> # (a ten, jack, queen, or king). >>> deck = collections.Counter(tens=16, low_cards=36) >>> seen = sample(list(deck.elements()), k=20) - >>> print(seen.count('tens') / 20) + >>> seen.count('tens') / 20 0.15 - # Estimate the probability of getting 5 or more heads from 7 spins - # of a biased coin that settles on heads 60% of the time. - >>> n = 10000 - >>> cw = [0.60, 1.00] - >>> sum(choices('HT', cum_weights=cw, k=7).count('H') >= 5 for i in range(n)) / n + >>> # Estimate the probability of getting 5 or more heads from 7 spins + >>> # of a biased coin that settles on heads 60% of the time. + >>> trial = lambda: choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5 + >>> sum(trial() for i in range(10000)) / 10000 0.4169 + >>> # Probability of the median of 5 samples being in middle two quartiles + >>> trial = lambda : 2500 <= sorted(choices(range(10000), k=5))[2] < 7500 + >>> sum(trial() for i in range(10000)) / 10000 + 0.7958 + Example of `statistical bootstrapping <https://en.wikipedia.org/wiki/Bootstrapping_(statistics)>`_ using resampling with replacement to estimate a confidence interval for the mean of a sample of |