Random Module In Python

Published: (December 22, 2025 at 10:37 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Overview

The random module in Python provides tools for generating randomness, such as random numbers, selecting items from sequences, and shuffling data. It’s useful for games, simulations, testing, and any situation where unpredictable results are needed.

Common Functions

random.randint(a, b)

Returns a random integer N such that a ≤ N ≤ b.

Example:

import random
print(random.randint(1, 10))  # random number between 1 and 10

random.choice(seq)

Selects a random element from a non‑empty sequence (e.g., list, tuple, string).

Use case: picking a random winner from a list of names.

random.random()

Returns a random floating‑point number x in the interval 0.0 ≤ x < 1.0.
Think of it as rolling a very small dice that always lands between 0 and 1.

random.shuffle(seq)

Shuffles the items of a mutable sequence in place, producing a new random order.

Typical use: randomizing a deck of cards.

random.seed(a=None)

Initializes the random number generator with a seed a. Providing the same seed yields repeatable results, which is handy for testing.

random.uniform(a, b)

Returns a random floating‑point number N such that a ≤ N ≤ b. Use it when you need a random decimal within a specific range.

random.sample(population, k)

Selects k unique elements from the population sequence without replacement. Useful for picking multiple random items without repeats.

Typical Use Cases

  • Games: rolling dice, shuffling cards, placing characters at random positions.
  • Random Selection: choosing a giveaway winner, selecting a random quiz question.
  • Simulations: modeling coin flips, weather patterns, or any stochastic process.
  • Data Generation: creating random passwords, test numbers, or random colors for visual projects.
Back to Blog

Related posts

Read more »