Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7fac12

Browse files
committed
Merge
2 parents ebbc736 + f5b7c7b commit c7fac12

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Doc/library/random.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ population with repeats::
329329

330330
>>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
331331
>>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
332+
>>> population
333+
['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
334+
332335
>>> random.choice(population)
333336
'Green'
334337

@@ -338,6 +341,9 @@ with :func:`itertools.accumulate`, and then locate the random value with
338341

339342
>>> choices, weights = zip(*weighted_choices)
340343
>>> cumdist = list(itertools.accumulate(weights))
344+
>>> cumdist # [3, 3+2, 3+2+1, 3+2+1+4]
345+
[3, 5, 6, 10]
346+
341347
>>> x = random.random() * cumdist[-1]
342348
>>> choices[bisect.bisect(cumdist, x)]
343349
'Blue'

0 commit comments

Comments
 (0)