summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2016-09-05 20:15:20 (GMT)
committerRaymond Hettinger <python@rcn.com>2016-09-05 20:15:20 (GMT)
commitc7fac12f193342ebe9954dd5b1c4e74db1be2ea8 (patch)
tree1b23e418f9c7c4748e4ec77ec2449bb479bd4e9d /Doc
parentebbc73692296e2d7efa9433166cbdd8f82411955 (diff)
parentf5b7c7bfc1876ac0f262f2f05952ec8841542f0a (diff)
downloadcpython-c7fac12f193342ebe9954dd5b1c4e74db1be2ea8.zip
cpython-c7fac12f193342ebe9954dd5b1c4e74db1be2ea8.tar.gz
cpython-c7fac12f193342ebe9954dd5b1c4e74db1be2ea8.tar.bz2
Merge
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/random.rst6
1 files changed, 6 insertions, 0 deletions
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 7c5b9e5..6dc54d2 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -329,6 +329,9 @@ population with repeats::
>>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
>>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
+ >>> population
+ ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
+
>>> random.choice(population)
'Green'
@@ -338,6 +341,9 @@ with :func:`itertools.accumulate`, and then locate the random value with
>>> choices, weights = zip(*weighted_choices)
>>> cumdist = list(itertools.accumulate(weights))
+ >>> cumdist # [3, 3+2, 3+2+1, 3+2+1+4]
+ [3, 5, 6, 10]
+
>>> x = random.random() * cumdist[-1]
>>> choices[bisect.bisect(cumdist, x)]
'Blue'