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.
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))2Practical Example
Here is a real-world application of random Module showing how it is used in production Python code.
import random
random.seed(7)
deck = ["A", "K", "Q", "J"]
random.shuffle(deck)
print(deck)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.
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))