🎲 Random Module
Last Updated: 20th August 2025
The random module is used to generate random numbers, select random items, shuffle data, and simulate randomness.
List of Random Functions:
- random(): Returns a random number between 0.0 and 1.0.
import random
print(random.random())
- uniform(a, b): Returns a random integer between a and b (inclusive) in float.
import random
print(random.uniform(1, 10))
- randint(a, b): Returns a random integer between a and b (inclusive).
import random
print(random.randint(1, 10))
- choice(sequence): Returns a random element from a sequence.
import random
fruits = ["apple", "banana", "cherry"]
print(random.choice(fruits))
- shuffle(sequence): Shuffles the elements of a sequence in place.
import random
fruits = ["apple", "banana", "cherry"]
random.shuffle(fruits)
print(fruits)
- randrange(start, stop, step): Returns a random integer between start and stop (exclusive) with a specified step.
import random
print(random.randrange(1, 10, 2))
- seed(value): Fixes the random sequence (for reproducibility in experiments)
import random
random.seed(42)
print(random.randint(1, 100))
print(random.randint(1, 100))
# Same results every time with seed=42