Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

random

Python's random module supplies pseudo-random values for distributions, integer ranges, and sequence operations. Its default Mersenne Twister generator is not suitable for security-sensitive randomness.

How it works

The module offers functions for numeric distributions and sequence operations such as choosing, shuffling, and sampling. Its default generator is based on Mersenne Twister, while SystemRandom obtains values from the operating system.

Syntax

import random
random.random()
random.randrange(start, stop[, step])
random.choice(seq)
random.sample(population, k, *, counts=None)
random.shuffle(x)
random.seed(a=None, version=2)

Parameters

NameTypeDescription
aoptional seed valueSeed value used to initialize the generator; the page documents seed() as a module function and supports an optional value.

Returns

Module functions return values appropriate to their operation, such as floats, integers, selected sequence elements, or modified sequences.

Key points

  • The module provides pseudo-random generators for several probability distributions.
  • Integer helpers can select uniformly from a specified range.
  • Sequence helpers include choosing an element, creating an in-place permutation, and sampling without replacement.
  • random() returns a uniformly distributed float from 0.0 inclusive up to, but not including, 1.0.
  • The default generator uses Mersenne Twister and is designed for general-purpose simulation rather than cryptographic protection.
  • SystemRandom provides an operating-system-backed alternative for generating random values.

Common mistakes

  • Do not use this module for passwords, tokens, or other security-sensitive values; use the secrets module instead.

Example: Selecting reproducible pseudo-random values

import random

random.seed(42)
items = ["red", "green", "blue"]
print(random.choice(items))
print(random.sample(items, k=2))
print(random.randrange(1, 10))

This seeds the default generator, selects one item, samples two distinct items, and chooses an integer from a half-open range.

When to use it

Sequence selection and permutation

Choose one or more items from a collection, or reorder a mutable sequence for a simulation.

Reproducible experiments

Generate repeatable inputs for tests, demos, and simulations by initializing the generator.

Distribution-based simulation

Produce values from supported probability distributions for modeling and Monte Carlo workloads.

Interview questions

QWhat kind of randomness does Python's random module provide?

The module generates pseudo-random values, with random() producing a float in the half-open interval from 0.0 through values below 1.0. Its core generator is Mersenne Twister.

Follow-up: How would you generate security-sensitive tokens? Use the secrets module rather than this module's pseudo-random generator.

QWhich operations support common sequence-randomization tasks?

Use randrange() for an integer selected from a range, choice() for one sequence element, sample() for distinct elements without replacement, and shuffle() to mutate a sequence in place.

Follow-up: What happens if sample() requests more elements than the population contains? It raises ValueError because sampling is without replacement.

QHow can results be made repeatable, and what is that useful for?

seed() initializes the generator's state, allowing repeatable pseudo-random sequences when the same supported seed is used. Reproducibility is useful for tests and simulations, but it does not make values secure.

Follow-up: How can an operating-system-backed generator be used? Instantiate SystemRandom, which uses os.urandom() as its source.

  • Random
  • SystemRandom
  • choice
  • choices
  • randrange
  • randint
  • sample
  • shuffle

Source: Random | Python documentation. Checked 2026-10-16.