random.random() / random.randint() / random.choice()
| Since: | Python 2(2000) |
|---|
Functions provided by the random module for generating random numbers, making random selections, and shuffling sequences. Commonly used in games, test data generation, and sampling.
Syntax
import random # Returns a random floating-point number in the range [0.0, 1.0). random.random() # Returns a random integer between a and b (inclusive). random.randint(a, b) # Returns a random floating-point number between a and b. random.uniform(a, b) # Returns a randomly chosen element from a sequence. random.choice(sequence) # Returns a list of k unique elements chosen from the sequence. random.sample(sequence, k) # Shuffles the list in place. random.shuffle(list)
Function List
| Function | Description |
|---|---|
| random.random() | Returns a random floating-point number in the range [0.0, 1.0). |
| random.randint(a, b) | Returns a random integer N such that a <= N <= b (both endpoints are included). |
| random.uniform(a, b) | Returns a random floating-point number between a and b. |
| random.choice(sequence) | Returns a randomly chosen element from a non-empty sequence (list, tuple, string, etc.). |
| random.sample(sequence, k) | Returns a new list of k unique elements chosen randomly from the sequence. The original sequence is not modified. |
| random.shuffle(list) | Shuffles the elements of a list in place. Returns None. |
| random.seed(value) | Sets the seed for the random number generator. Using the same seed produces the same sequence of random numbers. |
Sample Code
random_basic.py
import random
print(random.random())
dice = random.randint(1, 6)
print(f"Dice: {dice}")
temp = random.uniform(20.0, 30.0)
print(f"Temperature: {temp:.1f}°C")
Running the code produces the following output:
python3 random_basic.py 0.8444218515250481 Dice: 1 Temperature: 27.0°C
random_choice_sample.py
import random
fighters = ["Iori Yagami", "Kyo Kusanagi", "Terry Bogard", "Mai Shiranui", "Chris"]
picked = random.choice(fighters)
print(f"Picked fighter: {picked}")
winners = random.sample(fighters, k=3)
print(f"TOP3: {winners}")
kof_bgm = ["Arashi no Saxophone 2", "Wild Party", "Terry 115", "Mai", "Resurrection"]
print(f"Tonight's BGM: {random.choice(kof_bgm)}")
Running the code produces the following output:
python3 random_choice_sample.py Picked fighter: Kyo Kusanagi TOP3: ['Iori Yagami', 'Terry Bogard', 'Mai Shiranui'] Tonight's BGM: Arashi no Saxophone 2
random_shuffle_seed.py
import random cards = list(range(1, 14)) random.shuffle(cards) print(cards) random.seed(42) print(random.randint(1, 100))
Running the code produces the following output:
python3 random_shuffle_seed.py [10, 4, 5, 7, 3, 9, 6, 8, 2, 11, 1, 12, 13] 82
Common Mistakes
Common Mistake 1: Using the return value of shuffle()
random.shuffle() shuffles the list in place and returns None. Assigning the return value to a variable has no effect.
import random fighters = ["Iori Yagami", "Kyo Kusanagi", "Terry Bogard"] shuffled = random.shuffle(fighters) print(shuffled)
The same logic can also be written as:
import random fighters = ["Iori Yagami", "Kyo Kusanagi", "Terry Bogard"] random.shuffle(fighters) print(fighters)
Common Mistake 2: Using random for security-sensitive purposes
The random module is not suitable for use cases that require cryptographically secure random values, such as generating passwords or tokens. The secrets module is a safer choice for such purposes.
import random import string chars = string.ascii_letters + string.digits password = "".join(random.choice(chars) for _ in range(16)) print(password)
The same logic can also be written as:
import secrets import string chars = string.ascii_letters + string.digits password = "".join(secrets.choice(chars) for _ in range(16)) print(password)
Notes
Python's random module uses the Mersenne Twister algorithm to generate high-quality pseudo-random numbers. It is suitable for a wide range of applications such as games, simulations, and test data generation.
random.shuffle() modifies the list in place, so it returns None. If you want to keep the original list unchanged, make a copy before shuffling.
There are cases where the random module is not a good fit for security-sensitive purposes. When you need cryptographically secure random values — such as for passwords or tokens — the secrets module (e.g., secrets.token_hex() or secrets.choice()) is a safer option.
If you find any errors or copyright issues, please contact us.