"
This article is part of in the series
Published: Wednesday 15th February 2017
Last Updated: Thursday 30th December 2021

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.

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 therandom.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

The output of this program is as follows:

random.randint function 2

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

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

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

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

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

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

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

random.uniform function 2

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

random.uniform function 3

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

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

numpy.random.randint function 2

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

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

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

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.

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

The output is shown below:

secrets.randbelow function 2

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.