Generate Random Integers between 0 and 9 in Python

In this Python tutorial, we will learn how to generate random integers between 0 and 9 in Python?

Table Of Contents

Let’s dive into the tutorial.

Generate random numbers using random.randrange()

The randrange() method is available in the random python module, which returns a random number within the given range.

Syntax:

Advertisements
random.randrange(start,stop,step)

Parameters:

  1. start refers to the starting number. here it is 0.
  2. stop refers to the ending number. here it is 9.
  3. step will take an integer, which is used to skip the value between the given ranges.

Example:

In this example, we will generate a random number between 0 and 9.

from random import randrange

# Get random number in range 0-9
num = randrange(0,9)

print(num)

Output:

3

In the given range 0 to 9, number 3 is the random number returned.

Generate random numbers using random.randint()

The randint() method is available in the random Python module. It returns a random integer within the given range.

Syntax:

random.randint(start,stop)

Parameters:

  1. start refers to the starting number. here it is 0.
  2. stop refers to the ending number. here it is 9.

Example:

In this example, we will generate a random number in between 0 and 9.

from random import randint

# Get random number in range 0-9
num = randint(0,9)

print(num)

Output:

7

In the given range 0-9, number 7 is the random integer returned.

Generate random numbers using numpy.random.randint()

Suppose, if we want to return multiple random integers in the range of 0-9, then we can use the numpy module. It supports randint() and will return random integers in an array-[] separated by a comma.

Syntax:

numpy.random.randint(low, high, size=(n))

It will take three parameters.

  1. low refers to the starting range. here it is 0
  2. high refers to the ending range. here it is 9.
  3. size refers to the number of random integers returned within the given range.

Example:

In this example, we will generate 10 random numbers between 0 and 9.

import numpy 

# Get 10 random integers from 0 to 9
numbers = numpy.random.randint(low=0, high=9, size=(10)) 

print(numbers)

Output:

[5 0 3 0 7 1 5 4 2 6]

Generate random numbers using random.uniform()

The uniform() method is available in the random module, which will return a random float number within the given range. We have to typecast from float to integer using int().

Syntax:

random.int(uniform(low,high))

Parameters:

  1. low refers to the starting number. here it is 0.
  2. high refers to the ending number. here it is 9.

Example:

In this example, we will generate a random number in between 0 and 9.

from random import uniform

# Get random number
num = int(uniform(0, 9))

print(num)

Output:

3

In the given range 0-9, number 3 is the random integer returned.

Suppose, if we want to return multiple random integers in the range of 0-9, then we can use the numpy module. It supports uniform and will return random integers in an array, separated by a comma. Finally, we are typecasting from float to integer using the astype(int) method.

Syntax:

numpy.random.uniform(low, high, size=(n)).astype(int)

It will take three parameters.

  1. low refers to the starting range. here it is 0
  2. high refers to the ending range. here it is 9.
  3. size refers to the number of random integers returned within the given range.

Example:

In this example, we will generate 10 random numbers in between 0 and 9.

import numpy 

# Generate 10 random integers from 0 to 9
numbers = numpy.random.uniform(low=0, high=9, size=(10)).astype(int) 

print(numbers)

Output:

[3 6 2 2 3 4 4 3 8 8]

Generate Random Numbers using secrets.randbelow()

The randbelow() method is available in the secrets module. It returns a random number within the given range.

Syntax:

secrets.randbelow(value)

Parameters:

  • value is the maximum range.

Example:

In this example, we will generate a random number within range.

from secrets import randbelow

# Generate random number
num = randbelow(9)

print(num)

Output:

5

In the range 0 to 9, the number 5 is the random number returned.

Generate random number using numpy.random.choice()

The method numpy.random.choice() from numpy module is used to get n random numbers with in the range(value).

Syntax:

numpy.random.choice(value, size )

Parameters:

  1. value is the maximum range.
  2. size refers to the number of random integers returned within the given range(value).

Example:

In this example, we will generate 10 random numbers in the range of 0 to 9.

import numpy 

# Get 10 random integers 
numbers = numpy.random.choice(10, size=10 )

print(numbers)

Output:

[3 3 5 5 0 5 9 4 2 4]

Generate random numbers using random.sample()

The sample() method available in random python module. It is used to return the random numbers within the given range.

Syntax:

sample(range(start,stop), k)

It will take two parameters.

  1. range() will take two values
    • start refers to the start of range. Here it is 0.
    • stop refers to the end of range. Here it is 10.
  2. k refers to the number of random integers returned within the given range.

Example:

In this example, we will generate 10 random numbers in between 0 and 9.

from random import sample

# Get 10 random integers
numbers = sample(range(0,10),  k=10)

print(numbers)

Output:

[6, 7, 4, 3, 1, 5, 8, 2, 0, 9]

Summary

From the above article, we learned six different methods to generate random numbers in python. Using these methods, we can also use the numpy module to generate random numbers. Based on your requirement, you can use any of the above methods. Happy Learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top