typestar

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

  1. seed makes a run reproducible.
  2. choice, choices and sample differ in replacement and weights.
  3. shuffle works in place, sample returns a new list.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Numbers & text, step 60 of 72 in Language basics.

← Previous Next →