Skip to content

Commit 9cb30bb

Browse files
Move random selection recipes from itertools.rst to random.rst (GH-98369)
(cherry picked from commit 70732d8) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
1 parent 6c7f7ec commit 9cb30bb

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
@@ -564,6 +564,37 @@ Simulation of arrival times and service deliveries for a multiserver queue::
564564
Recipes
565565
-------
566566

567+
These recipes show how to efficiently make random selections
568+
from the combinatoric iterators in the :mod:`itertools` module:
569+
570+
.. testcode::
571+
import random
572+
573+
def random_product(*args, repeat=1):
574+
"Random selection from itertools.product(*args, **kwds)"
575+
pools = [tuple(pool) for pool in args] * repeat
576+
return tuple(map(random.choice, pools))
577+
578+
def random_permutation(iterable, r=None):
579+
"Random selection from itertools.permutations(iterable, r)"
580+
pool = tuple(iterable)
581+
r = len(pool) if r is None else r
582+
return tuple(random.sample(pool, r))
583+
584+
def random_combination(iterable, r):
585+
"Random selection from itertools.combinations(iterable, r)"
586+
pool = tuple(iterable)
587+
n = len(pool)
588+
indices = sorted(random.sample(range(n), r))
589+
return tuple(pool[i] for i in indices)
590+
591+
def random_combination_with_replacement(iterable, r):
592+
"Random selection from itertools.combinations_with_replacement(iterable, r)"
593+
pool = tuple(iterable)
594+
n = len(pool)
595+
indices = sorted(random.choices(range(n), k=r))
596+
return tuple(pool[i] for i in indices)
597+
567598
The default :func:`.random` returns multiples of 2⁻⁵³ in the range
568599
*0.0 ≤ x < 1.0*. All such numbers are evenly spaced and are exactly
569600
representable as Python floats. However, many other representable

0 commit comments

Comments
 (0)