Python Generate Random Number: 5 Common Use Cases

Minh Vu

By Minh Vu

Updated Nov 28, 2023

Figure: Python Generate Random Number: 5 Common Use Cases

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

Generating a random number is a common task in Python, especially when you are working on a Statistics or Machine Learning project.

In this article, I will show you how to generate random numbers in Python using the random module with 5 most common use cases that you will encounter most of the time.

Contents

Generating a Random Number Between 0 and 1

To generate a random number between 0 and 1 in Python, you can use the random.random function.

main.py
import random print(random.random())

The output will be a random number between 0 and 1.

console
0.8311963619648273
Generate Random Number Between 0 and 1 in Python
Figure: Generate Random Number Between 0 and 1 in Python

Generating a Random Number Between Two Numbers

To generate a random number between two numbers in Python, you can use the random.uniform function.

For example, to generate a random number (including real numbers) between 1 and 10, you can use the following Python code:

main.py
import random print(random.uniform(1, 10))

The output will be a random number between 1 and 10 based on the uniform distribution.

console
6.098467848075474
Generate Random Number Between 1 and 10 in Python
Figure: Generate Random Number Between 1 and 10 in Python

Generating a Random Integer Between Two Numbers

To generate a random integer between two numbers, you can use the random.randint function.

For example, to generate a random integer between 1 and 10, you can use the following Python code:

main.py
import random print(random.randint(1, 10))

The output will be a random integer between 1 and 10.

console
8
Generate Random Integer Between 1 and 10 in Python
Figure: Generate Random Integer Between 1 and 10 in Python

Generating a List of Random Numbers

To generate a list of random numbers in Python, you can use a for loop and put the above random functions inside it.

For example, to generate 5 random integers between 1 and 10, you can use the following Python code:

main.py
import random random_ints = [] for _ in range(5): random_ints.append(random.randint(1, 10)) print(random_ints)

We get 5 random integers between 1 and 10.

console
[1, 6, 7, 7, 10]
Generate List of Random Integers Between 1 and 10 in Python
Figure: Generate List of Random Integers Between 1 and 10 in Python

Generating a List of Random Integers using random.sample

Another way to generate a list of random integers in Python is to use the random.sample function.

For example, to generate 10 random integers between 1 and 100, you can use the following Python code:

main.py
import random print(random.sample(range(1, 100), 10))

The output will be a list of 5 random integers between 1 and 10.

console
[70, 54, 74, 63, 25, 83, 94, 76, 27, 14]
Generate 10 Random Integers Between 1 and 100 in Python
Figure: Generate 10 Random Integers Between 1 and 100 in Python

Conclusion

There are 5 common use cases when you want to generate random numbers in Python:

  1. Generate a random number between 0 and 1: random.random()
  2. Generate a random number between two numbers: random.uniform(a, b)
  3. Generate a random integer between two numbers: random.randint(a, b)
  4. Generate a list of random numbers: use a for loop with the above functions
  5. Generate a list of random integers: random.sample(range(a, b), n)
Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.