On this page
random.choice
Prev: random.binomialvariate | Up: random | Next: random.choices
Rel: random.betavariate, random.binomialvariate, random.choices, random.expovariate
Ref: docs.python.org — random.choice
Tags: #public #python3 #stdlib #random
Import
import random
from random import choice
Signature & Overview
random.choice(seq)
Choose a random element from a non-empty sequence.
Usage
import random
rng = random.Random(7)
pick = rng.choice(["red", "green", "blue"])
assert pick in ("red", "green", "blue")
Gotchas
- Requires a non-empty indexable sequence: Raises
IndexErroron empty sequences andTypeErroronset(convert withtuple(s)or usesample()).
REPL Inspect
from random import choice
help(choice)
Linked from
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/random/betavariate
- tuts/python3/stdlib_module/random/binomialvariate
- tuts/python3/stdlib_module/random/choices
- 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/choices 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