The random module generates pseudo-random numbers — they look random but are actually deterministic (if you set a seed, you get the same sequence every time). It uses the Mersenne Twister algorithm under the hood.
Important: Don't use this for security or cryptography — use the secrets module for that.
Everything else builds on random.random(), which returns a float between 0.0 and 1.0 (never quite reaching 1.0):
import random
random.random() # e.g., 0.37444887175646646randint(a, b)— random integer from a to b inclusive (both endpoints included)randrange(start, stop, step)— likerange()but picks one value randomly
random.randint(1, 6) # dice roll: 1, 2, 3, 4, 5, or 6
random.randrange(0, 100, 5) # 0, 5, 10, 15, ..., 90, or 95uniform(a, b)— random float between a and b
random.uniform(2.5, 10.0) # e.g., 3.1800146073117523choice(seq)— pick one random elementshuffle(seq)— rearrange a list in placesample(seq, k)— pick k unique elements without replacement
colors = ['red', 'blue', 'green', 'yellow']
random.choice(colors) # e.g., 'blue'
random.shuffle(colors) # modifies colors in place
random.sample(colors, 2) # e.g., ['green', 'red']choices(population, weights, k)— pick k elements with replacement, optionally weighted
# Pick 10 colors, but red is twice as likely as the others
random.choices(['red', 'blue', 'green'], weights=[2, 1, 1], k=10)Use random.seed() to get reproducible results — same seed = same sequence:
random.seed(42)
print(random.random()) # always 0.6394267984578837
print(random.random()) # always 0.025010755222666936
random.seed(42) # reset the seed
print(random.random()) # back to 0.6394267984578837This is super useful when you're iterating on an animation — you can get the same "random" variation every time you run it.
These generate numbers following statistical distributions — great for natural-looking variation:
gauss(mu, sigma)— normal (bell curve) distribution, centered atmuwith spreadsigmaexpovariate(lambd)— exponential distribution (good for timing events)triangular(low, high, mode)— triangular distribution (peaks atmode)
# Most values cluster around 50, with standard deviation of 10
random.gauss(50, 10) # e.g., 47.3, 52.1, 49.8
# Useful for creating "clumpy" spacing rather than uniform
random.expovariate(1.0 / 5.0) # average interval of 5Here's how you might use random in a sketch:
import py5
import random
def setup():
py5.size(800, 600)
py5.background(255)
# Set seed for reproducible randomness
random.seed(12345)
# Draw 50 circles with random positions and sizes
for _ in range(50):
x = random.uniform(0, py5.width)
y = random.uniform(0, py5.height)
diameter = random.gauss(30, 10) # average 30, some variation
# Pick a random color from your palette
color = random.choice(['#FF6B6B', '#4ECDC4', '#45B7D1'])
py5.fill(color)
py5.circle(x, y, diameter)randint(1, 10) includes both 1 and 10 (unlike range(1, 10) which stops at 9). This trips people up!
- Official Python documentation
- Use
random.seed()for reproducible "randomness" during development - For truly unpredictable numbers (security/crypto), use the
secretsmodule instead