• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Random Number in a Range in Python

Random Number in a Range in Python

Author: Aditya Raj
Last Updated: July 10, 2022

Python provides us with the random module to generate random numbers in our programs. In this article, we will discuss different ways to create a random number in a given range in python. 

Random Number in a Range Using the randint() Function

To create a random number in a range, we can use the randint() function. The randint() function takes the lower limit and upper limit of the range as the first and second input arguments respectively. After execution, it returns a random number between the lower limit and the upper limit. Here, the lower limit and upper limit are inclusive and the newly generated random number can be equal to the upper limit or the lower limit. You can create a random number in a range using the randint() function as follows.

import random

upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
number = random.randint(lower_limit, upper_limit)
print("The random number is:", number)

Output:

The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 739

Random Number in a Range Using the choice() Function

Instead of using the randint() function, we can use the choice() function defined in the random module to create a random number. For this, we will first create a sequence of numbers using the range() function. The range() function takes the lower limit of the sequence as its first argument, the upper limit of the sequence as its second argument, and the difference between two consecutive numbers in the sequence as the third optional argument. After execution it returns a list containing the sequence of the numbers. 

After creating the list, we will pass it as an input argument to the choice() function. The choice function takes the list as its input argument and returns a random number from the list. In this way, we can create a random number in a given range as shown below.

import random

upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
range_of_nums = range(lower_limit, upper_limit)
number = random.choice(range_of_nums)
print("The random number is:", number)

Output:

The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 848

To obtain more than one random number at a time, you can use the choices() function. The choices() function works exactly the same as the choice() function. Additionally, it takes the count of required random numbers as the second input argument and returns a list of specified random numbers as follows.

import random

upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
range_of_nums = range(lower_limit, upper_limit)
numbers = random.choices(range_of_nums, k=3)
print("The three random numbers are:", numbers)

Output:

The lower limit of the range is: 0
The upper limit of the range is: 1000
The three random numbers are: [105, 858, 971]

Using Numpy Module

We can also generate random numbers in a range using the functions defined in the numpy module.  For instance, you can use the numpy.random.randint() function instead of the random.randint() function to generate a random number as follows.

import numpy

upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
number = numpy.random.randint(lower_limit, upper_limit)
print("The random number is:", number)

Output:

The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 144

Similarly, you can use the numpy.random.choice() function to generate random numbers in a range as follows.

import numpy

upper_limit = 1000
lower_limit = 0
print("The lower limit of the range is:", lower_limit)
print("The upper limit of the range is:", upper_limit)
range_of_nums = range(lower_limit, upper_limit)
number = numpy.random.choice(range_of_nums)
print("The random number is:", number)

Output:

The lower limit of the range is: 0
The upper limit of the range is: 1000
The random number is: 264

Concussion

In this article, we have discussed different ways to generate a random number in a range in python. We have also discussed how to create a list of numbers. To know more about lists in python, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us