🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

random Module

AI & DATA SCIENCE // random-module

The random module generates pseudo-random numbers and performs random selections — sampling, shuffling, random integers and floats — using a deterministic algorithm seeded by system entropy by default.

Syntax

import random
random.random()
random.randint(1, 10)
random.choice(["a", "b", "c"])
random.shuffle(my_list)

Deep Dive Course

random.random() returns a float between 0.0 and 1.0, random.randint(a, b) returns an integer in that inclusive range, random.choice(seq) picks one random element from a sequence, and random.shuffle(list) reorders a list in place. Every function derives from the same underlying pseudo-random number generator, which is deterministic given a starting seed — calling random.seed(n) with a fixed value makes every subsequent 'random' call in the program reproducible, which is invaluable for writing repeatable tests or debugging.

1Understanding random Module

random.random() returns a float between 0.0 and 1.0, random.randint(a, b) returns an integer in that inclusive range, random.choice(seq) picks one random element from a sequence, and random.shuffle(list) reorders a list in place. Every function derives from the same underlying pseudo-random number generator, which is deterministic given a starting seed — calling random.seed(n) with a fixed value makes every subsequent 'random' call in the program reproducible, which is invaluable for writing repeatable tests or debugging.

💡

Never use the random module for anything security-sensitive, like generating passwords, tokens, or cryptographic keys — its pseudo-random output is predictable if an attacker can observe enough of it; use the secrets module instead for those cases.

editor.html
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
localhost:3000

2Practical Example

Here is a real-world application of random Module showing how it is used in production Python code.

editor.html
import random
random.seed(7)
deck = ["A", "K", "Q", "J"]
random.shuffle(deck)
print(deck)
localhost:3000

3Best Practices

Follow these guidelines when working with random Module:

1. Call random.seed(n) at the start of a test or script when you need reproducible 'random' behavior for debugging or automated tests

2. Use the secrets module, not random, for anything security-sensitive like tokens or password generation

3. Use random.choice()/random.sample() for picking from a collection instead of generating a random index manually with randint()

⚠️

Tip: Never use the random module for anything security-sensitive, like generating passwords, tokens, or cryptographic keys — its pseudo-random output is predictable if an attacker can observe enough of it; use the secrets module instead for those cases.

editor.html
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
localhost:3000

Examples

Example 01Basic Usage
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
Example 02Advanced Example
import random
random.seed(7)
deck = ["A", "K", "Q", "J"]
random.shuffle(deck)
print(deck)

Best Practices

  • Call random.seed(n) at the start of a test or script when you need reproducible 'random' behavior for debugging or automated tests
  • Use the secrets module, not random, for anything security-sensitive like tokens or password generation
  • Use random.choice()/random.sample() for picking from a collection instead of generating a random index manually with randint()

Interview Question

Why is the random module unsuitable for generating a secure password or authentication token?

Hint: Think about what 'pseudo-random' actually means and what an attacker could exploit.

random's number generator is pseudo-random: it's fully deterministic given its internal state, and that state can, in some cases, be reconstructed by an attacker who observes enough of its output, letting them predict future 'random' values. That predictability is exactly what you don't want for anything security-sensitive, like passwords or session tokens. The secrets module instead uses the operating system's cryptographically secure random source, specifically designed to resist this kind of prediction.

Exercises

MediumPractice using random Module in a real scenario.
View Solution
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))

Frequently Asked Questions

Why is the random module unsuitable for generating a secure password or authentication token?

random's number generator is pseudo-random: it's fully deterministic given its internal state, and that state can, in some cases, be reconstructed by an attacker who observes enough of its output, letting them predict future 'random' values. That predictability is exactly what you don't want for anything security-sensitive, like passwords or session tokens. The secrets module instead uses the operating system's cryptographically secure random source, specifically designed to resist this kind of prediction.

Related Functions

math-modulelist-comprehensionssets