# How to generate a random number in Python

## Introduction

Generating random numbers is a fundamental concept in programming and has a wide variety of applications such as creating unique IDs, generating passwords, and simulating real-world scenarios. In this tutorial, we will learn how to generate random numbers in Python using the `random` module provided by the Python standard library. We will also discuss some use cases for random number generation and how to generate random numbers within specific ranges or with specific constraints.

## What is a random number?

A random number is a number chosen by chance from a specified range or distribution. In other words, it's a number that cannot be predicted with certainty from the available data. In programming, random numbers are often generated using algorithms called pseudo-random number generators (PRNGs). These algorithms take an initial value (called a seed) and produce a sequence of numbers that appear to be random. However, it's essential to understand that the numbers produced by PRNGs are not truly random, but they are good enough for most purposes.

## The `random` module in Python

Python provides a built-in module called `random` that contains various functions to generate random numbers. To use the `random` module, you need to import it first:

``````import random
``````

Now, let's dive into some commonly used functions provided by the `random` module to generate random numbers.

## Generating a random float

The `random()` function in the `random` module generates a random float number between 0 (inclusive) and 1 (exclusive). Here's an example of how to use the `random()` function:

``````import random

random_float = random.random()
print("Random float between 0 and 1:", random_float)
``````

The output will be something like this:

``````Random float between 0 and 1: 0.2547145947132322
``````

Keep in mind that the output will be different each time you run the script, as it generates a new random number.

## Generating a random integer

To generate a random integer, we can use the `randint()` function in the `random` module. This function takes two arguments: the start and end of the range (both inclusive). Here's an example of how to generate a random integer between 1 and 10:

``````import random

random_integer = random.randint(1, 10)
print("Random integer between 1 and 10:", random_integer)
``````

The output may look like this:

``````Random integer between 1 and 10: 7
``````

Again, the output will vary each time you run the script.

## Generating a random number within a range

Sometimes, you may need to generate a random number within a specific range, but not necessarily an integer. In this case, you can use the `uniform()` function in the `random` module. This function takes two arguments: the start and end of the range, and it returns a random float number between the specified range. Here's an example of how to generate a random float number between 1 and 10:

``````import random

random_number = random.uniform(1, 10)
print("Random number between 1 and 10:", random_number)
``````

The output may look like this:

``````Random number between 1 and 10: 4.203948573210725
``````

## Generating a random number from a sequence

In some cases, you may need to generate a random number from a given sequence of numbers. The `random` module provides the `choice()` function for this purpose. This function takes a sequence (list, tuple, or string) as an argument and returns a randomly selected element from it. Here's an example of how to generate a random number from a list of numbers:

``````import random

numbers = [2, 4, 6, 8, 10]
random_number = random.choice(numbers)
print("Random number from the list:", random_number)
``````

The output may look like this:

``````Random number from the list: 6
``````

## Shuffling a sequence

Another useful function provided by the `random` module is the `shuffle()` function. This function takes a sequence (list or other mutable sequences) as an argument and shuffles its elements in place. This can be useful in applications such as card games or simulations where you need to randomize the order of elements in a sequence. Here's an example of how to shuffle a list of numbers:

``````import random

numbers = [1, 2, 3, 4, 5]
print("Original list:", numbers)

random.shuffle(numbers)
print("Shuffled list:", numbers)
``````

The output may look like this:

``````Original list: [1, 2, 3, 4, 5]
Shuffled list: [4, 1, 5, 2, 3]
``````

## Seeding the random number generator

As we mentioned earlier, PRNGs use a seed value to generate a sequence of random numbers. By default, Python uses the current system time as the seed value. However, you can set the seed value manually using the `seed()` function in the `random` module. This can be useful if you want to generate the same sequence of random numbers for testing or debugging purposes.

Here's an example of how to set the seed value for the random number generator:

``````import random

random.seed(42)  # Setting the seed value

random_number1 = random.random()
random_number2 = random.random()

print("Random numbers with seed 42:", random_number1, random_number2)

random.seed(42)  # Resetting the seed value

random_number3 = random.random()
random_number4 = random.random()

print("Random numbers with seed 42 (again):", random_number3, random_number4)
``````

The output will be:

``````Random numbers with seed 42: 0.6394267984578837 0.025010755222666937
Random numbers with seed 42 (again): 0.6394267984578837 0.025010755222666937
``````

As you can see, when we set the seed value to the same number, the generated random numbers are the same.

## Conclusion

In this tutorial, we learned how to generate random numbers in Python using the `random` module. We covered various functions provided by this module, such as `random()`, `randint()`, `uniform()`, `choice()`, `shuffle()`, and `seed()`. We also discussed some use cases for random number generation and how to generate random numbers within specific ranges or with specific constraints.

Now you should have a good understanding of how to generate and work with random numbers in Python. You can use this knowledge to create unique IDs, generate passwords, simulate real-world scenarios, and much more. Happy coding!

## Learn to code in our 100% online programs

Altcademy coding bootcamp offers beginner-friendly, online programs designed by industry experts to help you become a coder. 85%+ of Altcademy alumni are hired within 6 months after graduation. See how we teach, or click on one of the following programs to find out more.