Random numbers have very important applications in the world of programming. They can be used to create simulations, test cases for statistical or cryptoanalysis, for creating situations where we require a random or unpredictable result, and more.
In Python, we can generate a wide range of ragndom numbers using different modules and functions. In this article, we will discuss how to efficiently generate random numbers between 1 and 10 in Python.
Note that although our examples are limited to generated numbers between 1 and 10 in python, we can change this range to our desired value.
Table of Contents
- Using the random.randint() function
- Using the random.randrange() function
- Using the random.sample() function
- Using the random.uniform() function
- Using the numpy.random.randint() function
- Using the numpy.random.uniform() function
- Using the numpy.random.choice() function
- Using the secrets.randbelow() function
Using the random.randint()
function
When we talk about generating random numbers in Python, the random module comes to mind. This module has different functions to generate random numbers based on your requirement. The randint()
function returns a random integer between the start and end numbers provided within the function. It includes both the endpoints.
For example,
1 2 3 4 5 |
import random a = random.randint(1,10) print(a) |
Output:
In the above example, we return a random integer between 1 and 10. We can also generate a list of random numbers between 1 and 10 using the list comprehension method. List comprehension is a concise, elegant way to generate lists in a line of code.
For example,
1 2 3 4 5 |
import random a = [random.randint(1, 10) for i in range(0, 10)] print(a) |
Output:
In the above code, we use the list comprehension method. It uses a for loop to create a list with one line of code.
Using the random.randrange()
function
The randrange()
function is similar to the randint()
method. This function can also take a step
parameter, which can be thought of as the increment between the next number in the given range. This method doesn’t include the upper endpoint and can be used if only one number is specified in the function as it assigns the lower limit 0 by default.
For example,
1 2 3 4 5 6 7 |
import random a = random.randrange(1,10) print(a) lst = [random.randrange(1, 10) for i in range(0, 10)] print(lst) |
[5, 3, 8, 3, 9, 2, 9, 1, 5, 1]
As discussed earlier, we can use the list comprehension method to generate a list of random numbers between 1 and 10.
If we used the step
argument and set it to 2, we would get a result similar to the output shown below.
1 2 3 4 5 6 7 |
import random a = random.randrange(1,10, step = 2) print(a) lst = [random.randrange(0, 10, step = 2) for i in range(0, 10)] print(lst) |
Output:
[7, 3, 9, 9, 9, 9, 3, 9, 7, 9]
Using the random.sample()
function
The sample()
function is used to generate a list of desired size containing random numbers between a specified range. This function ensures that no number is repeated, that is why it requires the range to be bigger than the sample size otherwise an error is returned.
The following code shows its use to generate a single and a list of random numbers between 1 and 10.
1 2 3 4 5 6 7 |
import random a = random.sample(range(1,10),1) print(a) lst = random.sample(range(1,10),5) print(lst) |
Output:
Note that the final result in this method is always in a list, even while generating a single number.
Using the random.uniform()
function
The uniform()
method can be used if we want to return a random floating number between the provided range. It also includes both the limits.
For example,
1 2 3 4 5 6 7 |
import random a = random.uniform(1,10) print(a) lst = [random.uniform(1, 10) for i in range(0, 5)] print(lst) |
Output:
[4.579037661382095, 5.730055468585331, 8.1164519822083, 8.429507037067328, 9.871750857766747]
If we wish to get the final output as integers, then we can explicitly typecast the result of uniform()
function to an integer.
For example,
1 2 3 4 5 6 7 |
import random a = int(random.uniform(1,10)) print(a) lst = [int(random.uniform(1, 10)) for i in range(0, 5)] print(lst) |
Output:
[3, 3, 3, 2, 5]
Using the numpy.random.randint()
function
The numpy module also has a random sub module built inside which can be used to produce random numbers. It generates random numbers and stores them in a numpy array of the desired size and shape.
The numpy module can be a little faster than the random module when generating large amount of numbers.
The randint()
method works similar to the one in the random module, but stores the resultant in an array and we can specify the number of the random numbers we require.
For example,
1 2 3 4 5 6 7 |
import numpy as np a = np.random.randint(low=1, high=10) print(a) arr = a = np.random.randint(low=1, high=10, size = (5,)) print(arr) |
Output:
[8 1 3 7 5]
In the above example, we generated a random number and a one-dimensional array of required size containing random numbers between 1 and 10 in Python.
Using the numpy.random.uniform()
function
This is also similar to the one we already discussed with the main exception being the use of numpy module to store the result in an array with the size specified.
For example,
1 2 3 4 5 6 7 |
import numpy as np a = np.random.uniform(low=1, high=10) print(a) arr = a = np.random.uniform(low=1, high=10, size = (5,)) print(arr) |
Output:
[9.27276174 7.16017816 7.57330863 3.04637417 6.4280658 ]
As discussed earlier, we can have the final numbers as integers also.
Using the numpy.random.choice()
function
We can use numpy.random.choice()
to pick any number from list of 1 to 10 numbers. It uses the numpy.arange()
function to generate the required range and selects a number from that sequence and stores it in the array.
For example,
1 2 3 4 5 6 7 8 |
import numpy as np intList = [1, 2, 3, 4, 5, 6, 7, 8, 9] a = np.random.choice(intList) print(a) arr = a = np.random.choice(intList, size = (5,)) print(arr) |
Output:
[7 6 7 2 3]
Using the secrets.randbelow()
function
The secrets module is a relatively new addition in Python v3.6 and above. It is considered to be a more secure method to generate random numbers and has a vast use in cryptography. It can generate strong, secure random passwords, tokens, etc.
The randbelow()
function from this module generates a random number between 1 and the number specified in the function. We can use it to generate random numbers from 1 to 10.
For example,
1 2 3 4 |
from secrets import randbelow print(randbelow(10)) |
Output:
Here also, we can use the list comprehension method in the end if we want a list of such numbers.
That’s all about how to generate random number between 1 and 10 in Python.