The random module in Python
Seeded pseudo-randomness for simulations — never for secrets.
import random
random.seed(7)
print(random.randint(1, 6), random.random() < 1.0)
print(random.choice(["python", "rust", "sql"]))
print(random.choices("abc", weights=[5, 1, 1], k=5))
print(random.sample(range(10), k=3))
deck = list(range(5))
random.shuffle(deck)
print(deck)
print(round(random.gauss(100, 15), 2))
How it works
seedmakes a run reproducible.choice,choicesandsamplediffer in replacement and weights.shuffleworks in place,samplereturns a new list.
Keywords and builtins used here
listprintrangeround
The run, in numbers
- Lines
- 12
- Characters to type
- 313
- Tokens
- 124
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 78 seconds.
Step 2 of 3 in Numbers & text, step 60 of 72 in Language basics.