There is a need to generate random numbers when studying a model or behavior of a program for different range of values. Python can generate such random numbers by using the random module. In the below examples we will first see how to generate a single random number and then extend it to generate a list of random numbers. Show
Generating a Single Random NumberThe random() method in random module generates a float number between 0 and 1. Exampleimport random n = random.random() print(n) OutputRunning the above code gives us the following result − 0.2112200 Generating Number in a RangeThe randint() method generates a integer between a given range of numbers. Exampleimport random n = random.randint(0,22) print(n) OutputRunning the above code gives us the following result − 2 Generating a List of numbers Using For LoopWe can use the above randint() method along with a for loop to generate a list of numbers. We first create an empty list and then append the random numbers generated to the empty list one by one. Exampleimport random randomlist = [] for i in range(0,5): n = random.randint(1,30) randomlist.append(n) print(randomlist) OutputRunning the above code gives us the following result − [10, 5, 21, 1, 17] Using random.sample()We can also use the sample() method available in random module to directly generate a list of random numbers.Here we specify a range and give how many random numbers we need to generate. Exampleimport random #Generate 5 random numbers between 10 and 30 randomlist = random.sample(range(10, 30), 5) print(randomlist) OutputRunning the above code gives us the following result − [16, 19, 13, 18, 15]
Updated on 08-Aug-2019 06:54:57
Method 1 Here is a list of 10 elements in between
Why i used
Here is the execution of code in IPython.
Method 2 Here we use
Example of execution. " 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) 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: The output of this program is as follows: 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: The output is also shown in the code snippet given above. For loop can be used to generate a list. Using the ‘random.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: The code given here prints the single random value as well as the list of random values. The output is shown below: 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: 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: Using the ‘random.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. The output of this code is a floating-point number. To obtain the output as an integer, we can specially typecast the uniform() function. Typecasting the function gives integer output. Using the ‘numpy.random.randint()’ function :The numpy module also has the sub-module random. We 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: In the output of this code, we will obtain an array of random numbers. 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. Output: 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: Notice the arbitrary order of numbers in the user-entered list. Let's see what the output of the given code is: 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: The output is shown below: ConclusionThe 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.
How do you generate 10 random integers in Python?import random n = random. random() print(n). import random n = random. randint(0,22) print(n). import random randomlist = [] for i in range(0,5): n = random. randint(1,30) randomlist. ... . import random #Generate 5 random numbers between 10 and 30 randomlist = random. sample(range(10, 30), 5) print(randomlist). How can you generate random numbers in Python?To generate random number in Python, randint() function is used. This function is defined in random module.
How does Python generate 6 random numbers?“how to generate 6 random number in a function in python” Code Answer. # generate random integer values.. from random import randint.. value = randint(0, 10). print(value). |