Python Random Module: 6 Essential Methods for Generating Random Numbers

In our daily life when we play a ludo game and throw a dice any number can come between one to six. The number will be totally random we can not predict it. If we knew then definitely it would be no fun to us in playing those games. So that is what randomization is.

We can also develop these kinds of games by computer programming but how? For developing these kinds of games we need to create randomization and this can be achieved by an inbuilt module in Python which is called random module and it contains several functions through which we can create different random numbers.

Apart from this, we have one more important use of generating random numbers which is in machine learning as while creating datasets for the testing we need to generate random data so the random module plays an important role here as well.

In this article, we’ll look at six functions to generate random numbers using the Python random module. Let’s get started.

Python Random Module

The random module is a built-in module in Python’s Standard Library that provides functions for generating random numbers and working with randomness. It is an extremely practical module used for a lot of applications, games, and also in machine learning.

Random module functions for generating random numbers are given below:

  1. randint()
  2. randrange()
  3. random()
  4. uniform()
  5. choice()
  6. shuffle()

Let’s try to generate random numbers using them one by one.

1. Generating Random Numbers Using randint() Function

When we need to generate random integers between two numbers, let’s say a and b with both a and b being inclusive, we can use the function randint() of the random module.

Syntax:

random_num = random.randint(a, b)

Example:

import random
random_num = random.randint(1,7)
print(random_num)

In the above code, we first imported the random module then we provided parameters a as 1 and b as 7 in the random.randint() function and saved the result into a variable random_num and then printed the variable which will give us the random integer number between 1 and 7. We can also get 1 and 7 as both a and b are inclusive.

Output:

Generating Random Numbers Using randint() Function

2. Generating Random Numbers Using randrange() Function

The randrange() function generates a random integer from the specified range, it is similar to the range() function. We can specify a starting point let’s say start, an ending point let’s say stop, and step value let’s step.

Syntax:

random_num = random.randrange(start, stop, step)

Example:

import random
random_num = random.randrange(1,3)
print(random_num)

Here we have passed parameters start as 1 and stop as 3 in the random.randrange() function to get a random integer number between 1 and 3.

Output:

Generating Random Numbers Using randrange() Function

3. Generating Random Numbers Using random() Function

The random module also provides us with random() function which helps to generate random floating numbers between 0 and 1 where 0 will be inclusive and 1 will be exclusive. And the important thing is we don’t need to pass any argument in this function.

Syntax:

random_num = random.random()

Example:

import random
random_num = random.random()
print(random_num)

Here we just called the random.random() function and saved the result into the variable random_num and then printed the variable. The variable will contain a random float number between 0 and 1. Every time we run the above code we get different floating numbers.

Output:

Generating Random Numbers Using random() Function

4. Generating Random Numbers Using uniform() Function

The uniform() function is used to generate random floating numbers between two numbers, let’s say a and b which we need to pass as an argument in this function. Here a will be inclusive and b will be exclusive.

Syntax:

random_num = random.uniform(a, b)

Example:

import random
random_num = random.uniform(1,3)
print(random_num)

Here we have provided parameters a as 1 and b as 3 in the random.uniform() function which will return a random float number between 1 and 3.

Output:

Generating Random Numbers Using uniform() Function

5. Generating Random Numbers Using choice() Function

The choice() function offered by the random module is a little bit different from the other functions that we have discussed above. Here we have to provide a sequence as an argument and the sequence could be a list, tuple, or string and the choice function will return us any random element from the sequence.

Syntax:

random_num = random.choice(seq)

Example:

import random
l=[2,5,90,-5,89,12,56]
random_num = random.choice(l)
print(random_num)

Here we have passed a list to the random.choice() function. This function will return a random number from the list l. Every time we will run the above code we will get different random elements from the sequence.

Output:

Generating Random Numbers Using choice() Function

6. Generating Random Numbers Using shuffle() Function

The shuffle() function is used to shuffle the sequence that we have passed as an argument, i.e., it will change the position of the elements in the sequence.

Syntax:

random.shuffle(seq)

Example:

import random
l=[2,5,90,-5,89,12,56]
random.shuffle(l)
print(l)

Here we have shuffled or changed the position of the elements from the list l randomly. Every time we will run the above code the same element can be found at different positions in the sequence.

Output:

Generating Random Numbers Using shuffle() Function

Summary

The random module is very useful for a broad range of applications where randomness is required such as games, statistical analysis, and data science. In this tutorial, we have discussed six random module functions to generate random numbers with examples. After reading this tutorial, we hope you can easily generate random numbers in Python.

Reference

https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9

Priyanshu Singh
Priyanshu Singh
Articles: 44