Nearby lessons
132 of 159Python - Random Module
- Explain what the random module is and why unpredictable values are needed
- Generate floating-point numbers with random() and uniform()
- Generate random integers with randint() and randrange()
- Pick a random element from a list or tuple with choice()
- List the advantages and real-world applications of the random module
Introduction to random Module
Python provides an inbuilt module named random.
This module defines several functions to generate random numbers.
Using these functions, we can generate different random values whenever a program is executed.
Why Do We Use the random Module?
The random module is useful whenever unpredictable values are required.
We can use these functions while:
- Developing games.
- Cryptography.
- Generating random numbers on the fly for authentication.
Functions Covered in this Part
In this section, we will learn the following functions:
random()uniform()
1. random() Function
The random() function always generates a floating-point value between 0 and 1.
The generated value is always greater than 0 and less than 1.
Range of random()
Syntax
Example - random()
Sample Output
Explanation
The random() function generates a floating-point value every time it is called.
Each generated value lies between 0 and 1.
The value is never exactly 0 or 1.
Every execution of the program produces different values.
Important Note
Note: Every execution produces different values.
2. uniform() Function
The uniform(a, b) function returns a random floating-point number between the specified range.
Unlike random(), we can specify both the starting value and ending value.
Syntax
Example - uniform()
Sample Output
Explanation
The generated value may be any floating-point number between 1 and 10.
Every execution produces different values because the numbers are generated randomly.
This function is useful whenever random decimal values are required within a specified range.
Difference Between random() and uniform()
| random() | uniform(a, b) |
|---|---|
| Returns a floating-point value between 0 and 1. | Returns a floating-point value between the specified range. |
| Range is fixed. | Range is decided by the programmer. |
| No parameters. | Requires beginning and ending values. |
Real-World Applications
- Game development.
- OTP generation.
- Password generation.
- Lottery systems.
- Simulation software.
- Testing applications using random data.
- Cryptography-related programs.
Important Interview Questions
- What is the purpose of the
randommodule? - What type of value is returned by
random()? - What is the range of the
random()function? - What is the difference between
random()anduniform()? - Which function allows us to specify the range?
- Why do random numbers change every execution?
- Where is the
randommodule commonly used? - Can
uniform()generate decimal values?
Integer Random Functions
The random module also provides functions to generate random integer values.
Unlike random() and uniform(), these functions generate whole numbers (integers).
In this section, we will learn:
randint()randrange()
1. randint() Function
The randint(a, b) function returns a random integer between a and b.
Both the starting value and ending value are included in the generated range.
Syntax
Example - randint()
Sample Output
Explanation
The randint(1, 10) function generates a random integer between 1 and 10.
Both 1 and 10 are included in the possible output.
Each execution of the program produces different random numbers.
Important Note
- The generated value is always an integer.
- Both the beginning and ending values are included.
- Every execution produces different random values.
2. randrange() Function
The randrange() function returns a random integer from the specified range.
Its behavior is similar to Python's built-in range() function.
It is useful when random values are required from a sequence.
Syntax
Example 1
Sample Output
Explanation
The statement randrange(10) generates a random integer from 0 to 9.
The ending value 10 is not included.
Example 2
Sample Output
Explanation
The statement randrange(1, 11) generates a random integer between 1 and 10.
The ending value 11 is excluded.
Example 3
Sample Output
Explanation
The third parameter specifies the step value.
In this example, only odd numbers are generated because the step size is 2.
Possible values are:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19Difference Between randint() and randrange()
| randint() | randrange() |
|---|---|
| Returns a random integer. | Returns a random integer from a specified range. |
| Ending value is included. | Ending value is excluded. |
| No step value. | Supports an optional step value. |
| Simple syntax. | Works similar to the range() function. |
Real-World Applications
- Dice simulation.
- Lottery number generation.
- Card games.
- Quiz applications.
- OTP generation.
- Random test case generation.
- Gaming applications.
Important Interview Questions
- What is the difference between
randint()andrandrange()? - Does
randint()include the ending value? - Does
randrange()include the ending value? - Which function supports a step value?
- What is the output range of
randrange(10)? - How do you generate only odd numbers using the
randommodule? - Which function behaves like Python's
range()? - Name two functions used to generate random integers.
Sequence Functions in random Module
The random module also provides functions that work with sequences such as lists and tuples.
These functions do not always generate random numbers. Instead, they can select random elements from a sequence.
In this section, we will learn the choice() function.
5. choice() Function
The choice() function does not return a random number.
It returns a random object from the given list or tuple.
Every time the function is executed, it may return a different element from the sequence.
Syntax
Example
Sample Output
Explanation
The list contains five names.
Each time choice(list) is called, Python randomly selects one element from the list.
The selected element is returned and displayed.
The same element may appear multiple times because every selection is independent.
Important Points
choice()works with sequences such as lists and tuples.- It returns only one element at a time.
- The returned element is selected randomly.
- The same value can be selected multiple times.
- It does not generate a random number.
Comparison of Sequence Functions
| Function | Returns |
|---|---|
random() |
Random floating-point number between 0 and 1. |
uniform(a, b) |
Random floating-point number within the specified range. |
randint(a, b) |
Random integer between the specified range. |
randrange() |
Random integer from the specified range. |
choice() |
Random object from a list or tuple. |
Real-World Applications
- Selecting a random winner from a list.
- Displaying random motivational quotes.
- Choosing random quiz questions.
- Selecting random names in classrooms.
- Random card selection in games.
- Random recommendation systems.
Summary of random Module Functions
| Function | Description |
|---|---|
random() |
Returns a random float between 0 and 1. |
uniform(a, b) |
Returns a random float between a and b. |
randint(a, b) |
Returns a random integer between a and b. |
randrange() |
Returns a random integer from the specified range. |
choice() |
Returns a random object from a sequence. |
Important Interview Questions
- What is the purpose of the
choice()function? - Does
choice()return a random number? - Which data structures are supported by
choice()? - Can the same element be selected multiple times?
- What is the difference between
choice()andrandint()? - Name five important functions of the
randommodule. - Which function returns a random object from a sequence?
- Where is the
choice()function commonly used?
Advantages of random Module
- Easy to generate random values.
- Provides both integer and floating-point random numbers.
- Can randomly select elements from lists and tuples.
- Useful for simulations and testing.
- Reduces the need to implement custom random number algorithms.
- Built into Python, so no external installation is required.
Real-World Applications
- Game development.
- Lottery systems.
- Online quiz applications.
- Password and OTP generation.
- Authentication systems.
- Simulation software.
- Random testing and test data generation.
- Artificial Intelligence and Machine Learning experiments.
Important Interview Questions
- What is the purpose of the
randommodule? - Which function returns a floating-point number between 0 and 1?
- What is the difference between
random()anduniform()? - What is the difference between
randint()andrandrange()? - Which function returns a random object from a list?
- Can
choice()return duplicate values? - Which random function supports a step value?
- Which functions generate floating-point numbers?
- Which functions generate integers?
- List five functions available in the
randommodule. - Where is the
randommodule commonly used? - Is the
randommodule built into Python?
- The random module is built into Python and generates unpredictable values
- random() returns a float between 0 and 1, while uniform(a, b) returns a float within a specified range
- randint(a, b) includes the ending value, while randrange() excludes it and supports a step
- choice() selects a random element from a sequence such as a list or tuple
- The random module is used in games, cryptography, OTP and password generation, and simulations