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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.