Last Updated on April 10, 2023 by Prepbytes

Python Programming Language is gaining popularity among developers because of the wide range of features it provides to developers. One such task is Generating Random Numbers. Random Numbers are quite helpful when developing applications such as simulation games and some other scientific experiments. Python provides a variety of functions for generating random Numbers.

Here, we will discuss how can you generate random numbers in Python and also about the function to generate random number in Python. So, let’s begin the topic with the help of an example.

## Example of Python Random Number

Python has a module named random Module which contains a set of functions for generating and manipulating the random number. random() Function of the “random” module in Python is a pseudo-random number generator that generates a random float number between 0.0 and 1.0. Here is the demo code for the working of this function.

import random num = random.random() print(num)

**Output:**

`0.28558661066100943`

**Explanation:**

In the above code, we first import the random module so that we can use the random() Function. Next, we call the random() Function and store the result in the num variable, and printed it on the screen.

## How can you Generate Random Numbers in Python?

Here are some of the ways to generate Python Random Numbers.

### Method 1 of Python Random Number: Using random Module

As discussed in the above example, the random module has various methods for generating random numbers. The code given below uses different functions of the random module for generating random numbers.

import random # generate a random float between 0 and 1 random_number = random.random() print("Random Number using random(): ", random_number) # generate a random integer between two values (inclusive) random_integer = random.randint(1, 10) print("Random Number using randint(): ", random_integer) # generate a random float between two values random_float = random.uniform(1.0, 10.0) print("Random Number using uniform(): ", random_float)

**Output:**

```
Random Number using random(): 0.5947380988298357
Random Number using randint(): 9
Random Number using uniform(): 9.36409594669023
```

**Explanation:**

In the above code, we have used the three methods of the random module which are random(), randint(), and uniform().

- The random() Function generates a random float number between 0 and 1.
- The randint() Function is used to generate a random integer value between a given range of values.
- The uniform() Function of the random module is used for generating the float numbers between a given range of numbers.

All of these methods have been used in the above code and we get the random numbers on the screen.

### Method 2 of Python Random Number: Using numpy Module

Numpy Module in Python is used for scientific purposes and it also provides functions for generating random numbers. The two most common functions are:

numpy.random.rand()

numpy.random.randint()

**Code:**

import numpy as np # generate a random 1D array of floats between 0 and 1 random_array = np.random.rand(10) print("A random array using the rand() function:") print(random_array) print() # generate a random 2D array of integers between two values (inclusive) random_matrix = np.random.randint(1, 10, (3, 3)) print("A random 2-d Matrix") print(random_matrix)

**Output:**

```
A random array using the rand() function:
[0.56612004 0.09336885 0.20456486 0.38655937 0.73307193 0.54530791 0.94846686 0.77337937 0.52267922 0.93916112]
A random 2-d Matrix
[[8 7 6]
[9 6 5]
[4 3 5]]
```

**Explanation:**

In the above code, we have generated an array of length 10 and filled it with the random floating numbers between 0 and 1 by using the rand() Function. Next, we generated a 3×3 2D matrix and filled it with random integers between 1 and 10. Then, we printed both the matrix on the screen.

### Method 3 of Python Random Number: Using Secrets Module

Python 3.6+ provides a module named “secret”. It provides functions for generating cryptographically secure random numbers. The code given below illustrates its usage.

import secrets # generate a random integer below a given value random_integer = secrets.randbelow(10) print("Random value using the randbelow() Function: ", random_integer)

**Output:**

`Random value using the randbelow() Function: 8`

**Explanation:**

In the above code, we have used the randbelow() function of the secret module for generating the pseudo-random number.

### Method 4 of Python Random Number: Using random.choices Method

Now if you want to generate a random number or random element from a list, you can use the random.choices method. Here is the code example for better understanding.

import random # generate a random element from a given list random_element = random.choices(["apple", "banana", "cherry", "durian"]) print(random_element)

**Output:**

`['apple']`

**Explanation:**

In the above code, we use random.choices method on a list. The function returns a list containing a random element from the list. In our case, we get a list containing “apple” as an element.

**Conclusion**

In this article, we learned about different functions to generate random numbers in Python which are,

- random.random()
- random.randint()
- random.uniform()
- numpy.random.rand()
- numpy.random.randint()
- secrets.randbelow()
- random.choices()

We can choose any of the above methods for generating random numbers in Python based on our requirements.

## Frequently Asked Questions (FAQs)

Here are some Frequently Asked Questions on “How can you Generate Random Numbers in Python”.

**Ques 1. What is a pseudo-random number generator?**

**Ans.** The pseudo-random number generator is the algorithm that generates random numbers. These numbers appear to be random but are actually deterministic.

**Ques 2. How do I generate a random boolean value in Python?**

**Ans.** You can use the random.choice([True, False]) function in the random module to generate a random boolean value.

**Ques 3. How do you generate a random seed in Python?**

**Ans.** by using the random.seed() function, we can generate random seeds based on the current time.

**Ques 4. Can I generate truly random numbers in Python?**

**Ans. **Python’s built-in random module generates pseudo-random numbers, which are not truly random. To generate truly random numbers, you can use external hardware devices or web services that provide random numbers.