Nearby lessons

132 of 159

Python - Random Module

📌 What You Will Learn
  • 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:

  1. random()
  2. 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()

🐍Code Cell
10 < x < 1
Output
No output captured.

Syntax

🐍Code Cell
1from random import *
2 
3random()
Output
No output captured.

Example - random()

🐍Code Cell
1from random import *
2 
3for i in range(10):
4 print(random())
Output
No output captured.

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

🐍Code Cell
1uniform(begin, end)
Output
No output captured.

Example - uniform()

🐍Code Cell
1from random import *
2 
3for i in range(10):
4 print(uniform(1, 10))
Output
No output captured.

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

  1. What is the purpose of the random module?
  2. What type of value is returned by random()?
  3. What is the range of the random() function?
  4. What is the difference between random() and uniform()?
  5. Which function allows us to specify the range?
  6. Why do random numbers change every execution?
  7. Where is the random module commonly used?
  8. 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:

  1. randint()
  2. 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

🐍Code Cell
1randint(begin, end)
Output
No output captured.

Example - randint()

🐍Code Cell
1from random import *
2 
3for i in range(10):
4 print(randint(1, 10))
Output
No output captured.

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

🐍Code Cell
1randrange(begin, end)
2 
3or
4 
5randrange(begin, end, step)
Output
No output captured.

Example 1

🐍Code Cell
1from random import *
2 
3for i in range(10):
4 print(randrange(10))
Output
No output captured.

Sample Output

Explanation

The statement randrange(10) generates a random integer from 0 to 9.

The ending value 10 is not included.

Example 2

🐍Code Cell
1from random import *
2 
3for i in range(10):
4 print(randrange(1, 11))
Output
No output captured.

Sample Output

Explanation

The statement randrange(1, 11) generates a random integer between 1 and 10.

The ending value 11 is excluded.

Example 3

🐍Code Cell
1from random import *
2 
3for i in range(10):
4 print(randrange(1, 20, 2))
Output
No output captured.

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, 19

Difference 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

  1. What is the difference between randint() and randrange()?
  2. Does randint() include the ending value?
  3. Does randrange() include the ending value?
  4. Which function supports a step value?
  5. What is the output range of randrange(10)?
  6. How do you generate only odd numbers using the random module?
  7. Which function behaves like Python's range()?
  8. 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

🐍Code Cell
1choice(sequence)
Output
No output captured.

Example

🐍Code Cell
1from random import *
2 
3list = ["Sunny", "Bunny", "Chinny", "Vinny", "pinny"]
4 
5for i in range(10):
6 print(choice(list))
Output
No output captured.

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

  1. What is the purpose of the choice() function?
  2. Does choice() return a random number?
  3. Which data structures are supported by choice()?
  4. Can the same element be selected multiple times?
  5. What is the difference between choice() and randint()?
  6. Name five important functions of the random module.
  7. Which function returns a random object from a sequence?
  8. 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

  1. What is the purpose of the random module?
  2. Which function returns a floating-point number between 0 and 1?
  3. What is the difference between random() and uniform()?
  4. What is the difference between randint() and randrange()?
  5. Which function returns a random object from a list?
  6. Can choice() return duplicate values?
  7. Which random function supports a step value?
  8. Which functions generate floating-point numbers?
  9. Which functions generate integers?
  10. List five functions available in the random module.
  11. Where is the random module commonly used?
  12. Is the random module built into Python?
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

33 Questions
Progress: 0 / 33