On this page
random.choices
Prev: random.choice | Up: random | Next: random.expovariate
Rel: random.betavariate, random.binomialvariate, random.choice, random.expovariate
Ref: docs.python.org — random.choices
Tags: #public #python3 #stdlib #random
Import
import random
from random import choices
Signature & Overview
random.choices(population, weights=None, *, cum_weights=None, k=1)
Return a k sized list of population elements chosen with replacement.
Usage
import random
rng = random.Random(7)
draws = rng.choices(["common", "rare"], weights=[9, 1], k=5)
assert len(draws) == 5
Gotchas
- Samples WITH replacement: The same element can appear multiple times (use
random.sample()for sampling without replacement). - Use
cum_weights=in tight loops: Precomputing cumulative weights avoids recomputing them on every call.
REPL Inspect
from random import choices
help(choices)
Linked from
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/random/betavariate
- tuts/python3/stdlib_module/random/binomialvariate
- tuts/python3/stdlib_module/random/choice
- tuts/python3/stdlib_module/random/expovariate
- tuts/python3/stdlib_module/random/gammavariate
- tuts/python3/stdlib_module/random/gauss
- tuts/python3/stdlib_module/random/getrandbits
- tuts/python3/stdlib_module/random/getstate
- tuts/python3/stdlib_module/random/lognormvariate
- tuts/python3/stdlib_module/random/normalvariate
- tuts/python3/stdlib_module/random/paretovariate
- tuts/python3/stdlib_module/random/randbytes
- tuts/python3/stdlib_module/random/randint
- tuts/python3/stdlib_module/random/random
- tuts/python3/stdlib_module/random/Random_class
- tuts/python3/stdlib_module/random/randrange
- tuts/python3/stdlib_module/random/sample
- tuts/python3/stdlib_module/random/seed
- tuts/python3/stdlib_module/random/setstate
- tuts/python3/stdlib_module/random/shuffle
- tuts/python3/stdlib_module/random/SystemRandom
- tuts/python3/stdlib_module/random/triangular
- tuts/python3/stdlib_module/random/uniform
- tuts/python3/stdlib_module/random/vonmisesvariate
- tuts/python3/stdlib_module/random/weibullvariate
Related notes
- tuts/python3/stdlib_module/random/betavariate Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/binomialvariate Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/choice Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/expovariate Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/random Linked here; links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/gammavariate links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/gauss links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/getrandbits links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/getstate links to this page; shared tags: #random, #stdlib
- tuts/python3/stdlib_module/random/lognormvariate links to this page; shared tags: #random, #stdlib