"
This article is part of in the series
Published: Wednesday 15th February 2017
Last Updated: Thursday 6th February 2025

The aim of a programmer while creating a program is to ensure that the program stands the test for any random input. The program should correctly process the input and accurately calculate the output for any arbitrary data. A programmer must test the program for a wide range of information and amend the code accordingly to achieve this ambition.

Introduction to Random Numbers in Python

But how do you choose a random number from the infinite possibilities available to you? Even if you have got a set of numbers, how do you change it into an efficient program? Are there any in-built functions to achieve this? Well, the answer is Yes!

Python offers a large number of modules and functions to use random data. This article will guide you to include these functions in your code and provide code snippets for your convenience. We’ll keep our range limited to {1,10} but remember, you can use these methods and programming syntax for any range you prefer. Let’s get started!

Python library used: Anaconda Distribution (Jupyter notebook)

List Of Functions Available To Generate Random Numbers:

  • random.randint() function
  • random.randrange() function
  • random.sample() function
  • random.uniform() function
  • numpy.random.randint() function
  • numpy.random.uniform() function
  • numpy.random.choice() function
  • secrets.randbelow() function

Using the ‘random.randint() function:

random.randint() function is a part of the random module. The randint() function returns an integer value(of course, random!) between the starting and ending point entered in the function. This function is inclusive of both the endpoints entered.

We can write the code as follows:

random.randint function 1

import random
a=random.randint(1,10)
print(a)

The output of this program is as follows:

random.randint function 2

import random
a=random.randint(1,10)
print(a)
5

The function returns the number 5 as a random output. Note that here we have generated only a single random number. You can also create a list of random numbers. The code for generating a list of random numbers is as shown below:

random.randint function 3

import random
a=[random.randint(1,10) for i in range 0(0,10) ]
print(a)
[4, 9, 8, 4, 9, 5, 9, 3, 9, 4]

The output is also shown in the code snippet given above. For loop can be used to generate a list.

Using therandom.randrange() function :

This function is similar to the randint() function. This function includes the step parameter and excludes the upper limit entered in the function. The step parameter is optional and is used to exclude a particular value in the given range. The default value of this parameter is 1.

The syntax for using the randrange() function is as follows:
random.randrange function 1

import random
a=random.randrange(1,10)
print(a)
lst=[random.randrange(1,10) for i in range(0,10)]
print(lst)

The code given here prints the single random value as well as the list of random values.

The output is shown below:

random.randrange function 2

import random
a=random.randrange(1,10)
print(a)
lst=[random.randrange(1,10) for i in range(0,10)]
print(lst)
3
[1,7,7,4,2,5,8,7,1,9]

As we can see here, the first line gives a single-digit output, whereas the following line gives a list of values. 

Using the ‘random.sample() function :

We use the sample() function to create a list that has the length of our choice. Also, it returns a list that has no repeated values.

We can write the code as follows:

random.sample function 1

import random
a=random.sample(range(1,10),1)
print(a)
lst=random.sample(range(1,10),5)
print(lst)

Note that the syntax of this function uses the range() parameter to obtain the desired starting and ending values. Moreover, to generate a single-digit output, we have to enter ‘1’ as the list length beside the range() parameter. 

The output we obtain is as follows:

random.sample function 2

[6]
[9, 5, 6, 8, 4]

Using therandom.uniform()function :

We use the uniform() function to find floating-point numbers between the given range. This function is inclusive of both the endpoints of the given range.

The syntax of this code is the same as those mentioned for previous functions.

random.uniform function 1

import random
a=random.uniform(1,10)
print(a)

The output of this code is a floating-point number.

random.uniform function 2

8.467434810672394

To obtain the output as an integer, we can specially typecast the uniform() function.

random.uniform function 3

7

Typecasting the function gives integer output.

Using the numpy.random.randint() function :

The numpy module also has the sub-module randomWe can use the numpy module when we want to generate a large number of numbers. This module stores the output in an array of the desired size.

The randint() method is used similarly as in the random module. 

The syntax for this module is as follows:

numpy.random.randint function 1

import numpy as np
a = np.randon.randint(low=1, high=10)
print(a)
arr = a = np.random.randint(low=1, high=10, size=(6,))
print(arr)

In the output of this code, we will obtain an array of random numbers.

numpy.random.randint function 2

4
[4 8 5 1 3 3]

As you can see, an array of size six is generated.

Using the numpy.random.uniform() function :

The numpy module also has the uniform() function to generate an array of floating-point numbers.

numpy.random.uniform function 1

import numpy as np
a = np.random.uniform(low=1, high=10)
print(a)
arr = = np.random.uniform(low=1, high=10, size = (6,))
print(arr)

Output:

numpy.random.uniform function 2

Using the numpy.random.choice() function :

This function is used to obtain random numbers when we already have a list of numbers, and we have to choose a random number from that specific list. This function also stores the output in an array.

We can write the input of this function as follows:

numpy.random.choice function 1

import numpy as np
intlist = [1, 2, 3, 4, 6, 5, 9, 8, 7]
a = np.random.choice(intList)
print(a)
arr = a = np.random.choice(intList, size =(5,))
print(arr)

Notice the arbitrary order of numbers in the user-entered list.

Let's see what the output of the given code is:

numpy.random.choice function 2

9
[6 6 5 3 9]

Using the secrets.randbelow() function :

The secrets module is the most secure method to generate random numbers and finds excellent use in cryptography. One application of this module is to create strong, secure random passwords, tokens etc.

Setting a Range for Random Numbers

The randbelow()function is inclusive of both the limits entered in the function.

The syntax of this module is as follows:

secrets.randbelow function 1

from secrets import randbelow
print(randbelow(10))

The output is shown below:

secrets.randbelow function 2

How to Simulate Coin Flips

You can use the above method to simulate coin flips. If the range provided is just two consecutive integers, say 1 and 2, the script fetches only one value. If you assign heads and tails to each of the integer, you have successfully simulated a coin flip!

Rolling Dice with Random Numbers

With the similar method, you can provide the range of numbers from 1 to 6 to simulate the outcome of a dice roll. The opportunities are endless.

Using Random Numbers in Game Development

If you are working on a game development project, generating random numbers will become important to provide unpredictable or non-hard coded scenarios like the number of enemies in an RPG first person shooter game.

Why Generating a Random Integer is Necessary?

Either you are just using Python to select an item from the restaurant menu or working on your employer’s codebase, generating a random integer becomes necessary in many situations. Here are some of the situations:

  • Creating a shuffle program to pick random data.
  • Creating a secure password.
  • Developing a game that has unpredictable gameplays.
  • Cryptography: Generating secure keys and nonces to protect sensitive data.

Conclusion 

The use of random test cases is immense in programming. The built-in functions of several programming languages make it easier for us- the programmers to execute millions of arbitrary test cases and develop an efficient and accurate program. Python also offers a module for application in cryptography, as we have seen above. This module is beneficial if we want to create user-secure platforms. We hope this article helped you in learning the basics of random number generation. Keep an eye on PythonCentral because next we will cover uniform distribution, pareto distribution, gamma distribution, and so many more.