Generate random number between 1 and 10 in Python

Random number between 1 and 10 in Python

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.

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,

Output:

2

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,

Output:

[9, 4, 7, 10, 2, 6, 7, 0, 1, 1]

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,

3
[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.

Output:

9
[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.

Output:

[3] [7, 8, 9, 5, 3]

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,

Output:

9.318459320155256
[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,

Output:

1
[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,

Output:

4
[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,

Output:

2.162358774919478
[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,

Output:

2
[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,

Output:

3

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *