How to Generate Random Numbers in Python (random Module)
Learn how to generate random numbers in Python. Compare randrange, randint, and uniform float generation with seeding control.
Explanation
Generating random numbers is a key requirement in programming games, selecting random items from datasets, writing cryptography applications, and performing statistical simulations. Python provides a dedicated built-in library named `random` to handle pseudo-random number generation.
If you need a random integer within a specific range, the `random.randint(a, b)` function is the most common tool, returning a random integer where both endpoints are inclusive. Alternatively, `random.random()` generates a floating-point number between 0.0 (inclusive) and 1.0 (exclusive), which can be scaled mathematically to fit custom boundaries.
To make simulations and tests repeatable, you can use the `random.seed()` function. Providing a seed integer guarantees that the sequence of generated numbers will be identical every time the code runs. For cryptographic operations requiring high security (like token generation), standard pseudo-random generators should be avoided in favor of the `secrets` module.
Step-by-Step Implementation
- 1
Import the random module at the beginning of your script.
- 2
Use random.randint(min, max) to generate a random integer where both endpoints are inclusive.
- 3
Use random.random() to generate a random float between 0.0 and 1.0.
Code Example
This script demonstrates generating random integers, floats, and selecting elements with the random module.
import random
# Setting a seed for deterministic output in this example
random.seed(42)
# 1. Generate a random integer between 1 and 10 (inclusive)
random_int = random.randint(1, 10)
print("Random Integer (1-10):", random_int)
# 2. Generate a random float between 0.0 and 1.0
random_float = random.random()
print("Random Float (0-1):", round(random_float, 4))
# 3. Select a random element from a list
fruits = ["apple", "banana", "cherry"]
random_choice = random.choice(fruits)
print("Random Choice:", random_choice)Random Integer (1-10): 2
Random Float (0-1): 0.025
Random Choice: bananaFrequently Asked Questions
How do I generate a random float within a custom range?
Use random.uniform(a, b) to get a random floating-point number between a and b.
Is the random module secure for cryptographic use?
No, the random module is pseudo-random and should not be used for security-sensitive purposes. Use the secrets module instead.
Related How-To Guides
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.