Skip to content

Commit 70732d8

Browse files
authored
Move random selection recipes from itertools.rst to random.rst (GH-98369)
1 parent 5fe0431 commit 70732d8

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

Doc/library/itertools.rst

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,31 +1000,6 @@ which incur interpreter overhead.
10001000
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
10011001
return next(filter(pred, iterable), default)
10021002

1003-
def random_product(*args, repeat=1):
1004-
"Random selection from itertools.product(*args, **kwds)"
1005-
pools = [tuple(pool) for pool in args] * repeat
1006-
return tuple(map(random.choice, pools))
1007-
1008-
def random_permutation(iterable, r=None):
1009-
"Random selection from itertools.permutations(iterable, r)"
1010-
pool = tuple(iterable)
1011-
r = len(pool) if r is None else r
1012-
return tuple(random.sample(pool, r))
1013-
1014-
def random_combination(iterable, r):
1015-
"Random selection from itertools.combinations(iterable, r)"
1016-
pool = tuple(iterable)
1017-
n = len(pool)
1018-
indices = sorted(random.sample(range(n), r))
1019-
return tuple(pool[i] for i in indices)
1020-
1021-
def random_combination_with_replacement(iterable, r):
1022-
"Random selection from itertools.combinations_with_replacement(iterable, r)"
1023-
pool = tuple(iterable)
1024-
n = len(pool)
1025-
indices = sorted(random.choices(range(n), k=r))
1026-
return tuple(pool[i] for i in indices)
1027-
10281003
def nth_combination(iterable, r, index):
10291004
"Equivalent to list(combinations(iterable, r))[index]"
10301005
pool = tuple(iterable)

Doc/library/random.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,37 @@ Simulation of arrival times and service deliveries for a multiserver queue::
582582
Recipes
583583
-------
584584

585+
These recipes show how to efficiently make random selections
586+
from the combinatoric iterators in the :mod:`itertools` module:
587+
588+
.. testcode::
589+
import random
590+
591+
def random_product(*args, repeat=1):
592+
"Random selection from itertools.product(*args, **kwds)"
593+
pools = [tuple(pool) for pool in args] * repeat
594+
return tuple(map(random.choice, pools))
595+
596+
def random_permutation(iterable, r=None):
597+
"Random selection from itertools.permutations(iterable, r)"
598+
pool = tuple(iterable)
599+
r = len(pool) if r is None else r
600+
return tuple(random.sample(pool, r))
601+
602+
def random_combination(iterable, r):
603+
"Random selection from itertools.combinations(iterable, r)"
604+
pool = tuple(iterable)
605+
n = len(pool)
606+
indices = sorted(random.sample(range(n), r))
607+
return tuple(pool[i] for i in indices)
608+
609+
def random_combination_with_replacement(iterable, r):
610+
"Random selection from itertools.combinations_with_replacement(iterable, r)"
611+
pool = tuple(iterable)
612+
n = len(pool)
613+
indices = sorted(random.choices(range(n), k=r))
614+
return tuple(pool[i] for i in indices)
615+
585616
The default :func:`.random` returns multiples of 2⁻⁵³ in the range
586617
*0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly
587618
representable as Python floats. However, many other representable

0 commit comments

Comments
 (0)