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?


import_contacts

You may also like:

Related Posts

  • 23 September

    Convert 0 to 1 and 1 to 0 in Python

    Table of ContentsWays to convert 0 to 1 and 1 to 0 in PythonUsing SubtractionUsing XORUsing not operatorUsing lambda expressionUsing addition and remainder operatorConvert 1 to 0 and 0 to 1 in listConvert 1 to 0 and 0 to 1 in numpy arrayUsing where with bitwise xorUsing bitwise operators In this post, we will see […]

  • 23 September

    TypeError: Object of Type Datetime Is Not Json Serializable in Python

    Table of ContentsFix the object of type datetime is not JSON serializable exception in PythonUsing the default parameter in the json.dumps() functionUsing the cls parameter in the json.dumps() functionUsing the str functionConclusion In Python, the datetime library allows us to create objects of the datetime class that can store and process date and time values […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 23 September

    Convert Datetime to Unix Timestamp in Python

    Table of ContentsDates and Unix TimestampWays to convert datetime to Unix timestampUsing the total_seconds() function to convert datetime to Unix timestamp in PythonUsing the timestamp() function to convert datetime to Unix timestamp in PythonUsing the strftime() function to convert datetime to Unix timestamp in PythonUsing the time.mktime() function to convert datetime to Unix timestamp in […]

  • 23 September

    Find All Substrings of String in Python

    Table of ContentsHow To Find All Substrings of String in Python?Using a User-Defined Function that Makes Use of String Slicing to Find All Substrings of String in Python.Using List Comprehension Along with String Slicing to Find All Substrings of String in Python.Using the itertools.Combinations() Function to Find All Substrings of String in Python. Dealing with […]

  • 21 September

    Print Bytes without b in Python

    Table of ContentsWays to Print Bytes without b in PythonUsing the decode() Function to Print Bytes without b in PythonUsing the str() Function to Print Bytes without b in PythonUsing the repr() Function to Print Bytes without b in PythonConclusion Just as a string is a collection of Unicode characters in Python 3, bytes are […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.