How do you print 10 random numbers in python?


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.

Generating a Single Random Number

The random() method in random module generates a float number between 0 and 1.

Example

import random
n = random.random()
print(n)

Output

Running the above code gives us the following result −

0.2112200

Generating Number in a Range

The randint() method generates a integer between a given range of numbers.

Example

import random
n = random.randint(0,22)
print(n)

Output

Running the above code gives us the following result −

2

Generating a List of numbers Using For Loop

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

Example

import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist.append(n)
print(randomlist)

Output

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

Example

import random
#Generate 5 random numbers between 10 and 30
randomlist = random.sample(range(10, 30), 5)
print(randomlist)

Output

Running the above code gives us the following result −

[16, 19, 13, 18, 15]

How do you print 10 random numbers in python?

Updated on 08-Aug-2019 06:54:57

  • Related Questions & Answers
  • Generating Random Prime Number in JavaScript
  • Generating random number in a range in C
  • Generating random Id’s in Python
  • Generating random numbers in Java
  • Generating random numbers in C#
  • Generating Random String Using PHP
  • Generating Random id's using UUID in Python
  • Generating random hex color in JavaScript
  • Generating Random Short Id in Node.js
  • Generating a random number that is divisible by n in JavaScript
  • Generating random string of specified length in JavaScript
  • Generating random strings until a given string is generated using Python
  • Random Number Functions in Python
  • Generating n random numbers between a range - JavaScript
  • Generating random string with a specific length in JavaScript

Method 1 Here is a list of 10 elements in between num1 and num2

[random.randrange(*sorted([num1,num2])) for i in range(10)]

Why i used sorted

random.randrange only take increasing order of number

Here is the execution of code in IPython.

In [1]: import random
In [2]: num1 = int(input("Enter your first number: "))
Enter your first number: 10
In [3]: num2 = int(input("Enter your second number: "))
Enter your second number: 100
In [4]: for rand_num in [random.randrange(*sorted([num1,num2])) for i in range(10)]:
   .....:     print rand_num
   .....:     
69
63
14
22
98
39
66
21
26
72

Method 2

Here we use random.sample. If the range(num1,num2) have more than 10 population then only this method is valid.

random.sample(range(*sorted([num1,num2])),10)

Example of execution.

In [1]: random.sample(range(1,100),10)
Out[1]: [55, 5, 91, 19, 11, 42, 37, 45, 79, 86]

"

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 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:

How do you print 10 random numbers in python?

The output of this program is as follows:

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

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

The output is shown below:

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

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.

How do you print 10 random numbers in python?

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

How do you print 10 random numbers in python?

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

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

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

How do you print 10 random numbers in python?

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.

How do you print 10 random numbers in python?

Output:

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

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

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

How do you print 10 random numbers in python?

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:

How do you print 10 random numbers in python?

The output is shown below:

How do you print 10 random numbers in python?

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. 

  1. Home
  2. Python How To's

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