Skip to content

Python random Module: Detailed Overview and Examples

The random module in Python provides functions for generating random numbers and performing random operations. This module implements pseudo-random number generators for various distributions, making it a versatile tool for simulations, games, security, and more.

Importing the random Module

To use the functions from the random module, you need to import it:

import random

Key Functions and Methods

1. Generating Random Numbers

random.random()

Returns a random floating-point number in the range [0.0, 1.0).

Example
import random

print(random.random())  # Output: 0.37444887175646646 (example)

random.uniform(a, b)

Returns a random floating-point number in the range [a, b] or [b, a].

Example
import random

print(random.uniform(1, 10))  # Output: 5.422116796127306 (example)

random.randint(a, b)

Returns a random integer N such that a <= N <= b.

Example
import random

print(random.randint(1, 10))  # Output: 7 (example)

random.randrange(start, stop[, step])

Returns a randomly selected element from range(start, stop, step).

Example
import random

print(random.randrange(0, 10, 2))  # Output: 4 (example)

2. Working with Sequences

random.choice(seq)

Returns a random element from the non-empty sequence seq.

Example
import random

colors = ['red', 'blue', 'green', 'yellow']
print(random.choice(colors))  # Output: 'blue' (example)

random.choices(population, weights=None, *, cum_weights=None, k=1)

Returns a list of k elements chosen from the population with replacement. weights or cum_weights can be used to influence the selection.

Example
import random

colors = ['red', 'blue', 'green', 'yellow']
print(random.choices(colors, k=3))  # Output: ['green', 'yellow', 'red'] (example)

random.sample(population, k)

Returns a list of k unique elements chosen from the population without replacement.

Example
import random

colors = ['red', 'blue', 'green', 'yellow']
print(random.sample(colors, 3))  # Output: ['blue', 'yellow', 'red'] (example)

random.shuffle(x[, random])

Shuffles the sequence x in place.

Example
import random

numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers)  # Output: [3, 5, 1, 4, 2] (example)

3. Generating Random Values from Distributions

random.gauss(mu, sigma)

Returns a random float from a Gaussian (normal) distribution with mean mu and standard deviation sigma.

Example
import random

print(random.gauss(0, 1))  # Output: 0.22873166603286567 (example)

random.expovariate(lambd)

Returns a random float from an exponential distribution with rate lambd.

Example
import random

print(random.expovariate(1.5))  # Output: 0.36827364554614023 (example)

random.triangular(low, high, mode)

Returns a random float from a triangular distribution within the range [low, high] with the specified mode.

Example
import random

print(random.triangular(0, 10, 5))  # Output: 4.642715463090914 (example)

random.betavariate(alpha, beta)

Returns a random float from a Beta distribution with parameters alpha and beta.

Example
import random

print(random.betavariate(2, 5))  # Output: 0.17193864448253458 (example)

random.gammavariate(alpha, beta)

Returns a random float from a Gamma distribution with shape parameter alpha and scale parameter beta.

Example
import random

print(random.gammavariate(2, 2))  # Output: 3.545345143949921 (example)

random.lognormvariate(mu, sigma)

Returns a random float from a log-normal distribution with mean mu and standard deviation sigma.

Example
import random

print(random.lognormvariate(0, 1))  # Output: 0.9185488656444778 (example)

random.weibullvariate(alpha, beta)

Returns a random float from a Weibull distribution with scale alpha and shape beta.

Example
import random

print(random.weibullvariate(1, 1.5))  # Output: 0.6329110645226275 (example)

Seeding the Random Number Generator

random.seed(a=None, version=2)

Initializes the random number generator. The a argument can be any hashable object. If a is None, the current system time is used.

Example
import random

random.seed(42)
print(random.random())  # Output: 0.6394267984578837 (example)

Practical Examples

Example 1: Simulating a Dice Roll

import random

def roll_dice():
    return random.randint(1, 6)

print(roll_dice())  # Output: 4 (example)

Example 2: Selecting a Random Password

import random
import string

def generate_password(length):
    characters = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(characters) for _ in range(length))

print(generate_password(10))  # Output: 'g#8N!d2P&5' (example)

Example 3: Shuffling a Deck of Cards

import random

deck = [f"{rank}{suit}" for suit in "♠♥♦♣" for rank in "A23456789JQK"]
random.shuffle(deck)
print(deck)  # Output: ['6♣', 'Q♦', '5♦', '3♠', 'K♥', '2♣', ...] (example)

Conclusion

The random module in Python is a powerful tool for generating random numbers and performing random operations. From simple random number generation to more complex random sampling and distribution functions, the random module offers a wide range of utilities for various applications. Whether you're simulating data, creating random passwords, or building games, the random module provides the functionality you need to introduce randomness into your programs.