tech beamers
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
    • Python
    • C
    • Java
    • MySQL
    • Linux
    • Web
    • Android
    • AngularJS
    • Playwright
    • Selenium
    • Agile
    • Testing
    • Automation
    • Best IDEs
    • How-To
    • Technology
    • Gaming
    • Branding
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
    • Python Interview
    • SQL Query Interview
    • SQL Exercises
    • Selenium Interview
    • Playwright Interview
    • QA Interview
    • Manual Testing
    • Rest API Interview
    • Linux Interview
    • CSharp Interview
    • Python Function Quiz
    • Python String Quiz
    • Python OOP Quiz
    • Python DSA Quiz
    • ISTQB Quiz
    • Selenium Quiz
    • Java Spring Quiz
    • Java Collection Quiz
    • JavaScript Quiz
    • Shell Scripting Quiz
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
    • Online Python Compiler
    • Python Code Checker
    • Python Code Quality
    • Username Generator
    • Insta Password Generator
    • Google Password Generator
    • Free PDF Merger
    • QR Code Generator
    • Net Worth Calculator
tech beamers
Search
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Python Examples

Python Program: Generate Random Integer

Last updated: Mar 30, 2025 2:35 am
Meenakshi Agarwal
By Meenakshi Agarwal
No Comments
6 months ago
Share
7 Min Read
SHARE

In this sample program, you will learn to generate random integer numbers and show the result using the print() function.

Contents
  • Pre-requisites to generate random integers
    • Import the random module
    • Steps to generate a random integer
  • Python programs to generate random integers
    • Single random integer program
    • Multiple random integers program
    • Generate random integers within a range
    • Generate random integers for a random range
    • Python program using random choices
    • Python program to set a random seed
    • Where to use random numbers?
  • Key takeaways from Python random integer programs
Generate Random Integer Numbers

Pre-requisites to generate random integers

If you are new to Python, we suggest to check our basic Python programming tutorial. Also, read this Python guide on random numbers to learn about them.

    Import the random module

    To get started, you need to import the random module. This module offers a range of functions for generating random data. Import it at the beginning of your Python script like this:

    import random

    Steps to generate a random integer

    In a random number generator program, you need to ask user to input two inputs and store them in different variables.

    • Total number of random numbers to generate
    • The Upper Limit for the random numbers

    To generate random integer numbers, you can use Python’s random module and one of its functions known as the randint().

    Python programs to generate random integers

    Here, we have provided several python programs handling different random number generation scenarios. Please read and execute them one-by-one to learn thoroughly.

    Single random integer program

    The most straightforward way to generate a single random integer is to use the random.randint() function. It takes two arguments: the lower and upper bounds for the generated number (inclusive). Here’s an example:

    import random
    
    # Find a random integer between 3 and including 333
    random_number = random.randint(3, 333)
    
    print("Random Integer:", random_number)

    Multiple random integers program

    # Program to generate random integer numbers
    
    # Import Random Module
    import random
    
    count = int(input("How many random numbers do you want to generate? "))
    rmax = int(input("Enter Upper Limit for the random numbers: "))
    
    for r in range(count):
        print(random.randint(0, rmax))

    The output of the above code is as follows:

    How many random numbers do you want to generate? 10
    Enter Upper Limit for the random numbers: 1000
    703
    153
    247
    481
    971
    505
    794
    589
    968
    126

    Generate random integers within a range

    Suppose you need random integers within a specific range. In that case, you can use the random.randrange() function, which takes three arguments: start, stop, and step. The start / step arguments are optional.

    Here’s an example that generates random integers between 10 and 50 (inclusive) with a step of 5:

    import random
    
    # Generate random integers between 10 and 50 (inclusive) with a step of 5
    for _ in range(10):
        random_number = random.randrange(10, 55, 5)
        print("Random Integer:", random_number)

    In this code, random.randrange(10, 55, 5) will yield random integers between 10 and 55 (inclusive) with intervals of 5.

    Generate random integers for a random range

    To add an element of surprise, you can generate random integers within a dynamically determined range. Here’s how:

    import random
    
    # Determine a random range for the random integers
    lower_bound = random.randint(1, 10)
    upper_bound = random.randint(11, 20)
    
    # Generate a random integer within the determined range
    random_number = random.randint(lower_bound, upper_bound)
    
    print("Lower Bound:", lower_bound)
    print("Upper Bound:", upper_bound)
    print("Random Integer within Range:", random_number)

    In this example, lower_bound and upper_bound are randomly defined, and a random integer is generated within that range.

    Python program using random choices

    In some cases, you might want random integers selected from a predefined sequence of values. The functionrandom.choice() is perfect for this:

    import random
    
    # Define a list of possible random integer values
    integer_list = [10, 20, 30, 40, 50]
    
    # Generate a random integer from the list
    random_number = random.choice(integer_list)
    
    print("Random Integer from List:", random_number)

    This code snippet will pick a random integer from the integer_list.

    Python program to set a random seed

    By default, Python’s random module generates pseudorandom numbers. If you want to reproduce the same sequence of random numbers in the future, you can set a seed using random.seed(). This is especially useful for debugging and testing. Here’s an example:

    import random
    
    # Set a seed for reproducibility
    random.seed(42)
    
    # Generate random integers
    for _ in range(5):
        random_number = random.randint(1, 100)
        print("Random Integer:", random_number)

    In this example, setting the seed to 42 ensures that the same sequence of random integers will be generated each time the script is run.

    Where to use random numbers?

    Random integer numbers are useful in a variety of Python applications. For example, they play an important role in creating games, simulations, and statistical models.

    Here are a few examples of how you can use random integer numbers in Python applications:

    • Create a dice game: You can use the randint() function to generate random numbers between 1 and 6 to simulate the roll of a die.
    • Create a random number generator: The randint() function can also be used to create a random number generator for a variety of purposes, such as generating passwords or creating random samples of data.
    • Create a simulation: You can use random integer numbers to create simulations of real-world issues such as to simulate the spread of a disease or the growth of a population.
    • Create a statistical model: You can use random integer numbers to generate statistical models from real-world data. For example, you can build a model to show the income of a group or to visualize customer data that visits a store each day.

    Key takeaways from Python random integer programs

    Congratulation. Today, you have learned different ways to generate random integers in Python. This is a common task in many applications. Python’s random module simplifies this process.

    On top of it, we taught you how to set a seed value so that you can always produce the same sequence of random numbers. It is sometimes required for testing and debugging purposes.

    Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

    Enjoy Coding,
    TechBeamers

    Related

    TAGGED:Random Data Generation Made Easy
    Share This Article
    Whatsapp Whatsapp LinkedIn Reddit Copy Link
    Meenakshi Agarwal Avatar
    ByMeenakshi Agarwal
    Follow:
    Meenakshi Agarwal, Founder of TechBeamers.com, holds a BSc from Lucknow University and MCA from Banasthali University. With 10+ years as a Tech Lead at Aricent Technologies, she delivers expert Python, Java, Selenium, SQL, and AI tutorials, quizzes, and interview questions.
    Previous Article Python program to swap two numbers Python Program: Swap Two Numbers Without a Temp
    Next Article Check If Python List Contains Elements Of Another List Python Program: Check List Contains Another List Items
    Leave a Comment

    Leave a Reply

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

    List of Topics

    Stay Connected

    FacebookLike
    XFollow
    YoutubeSubscribe
    LinkedInFollow

    Continue Reading

    • Python Program: Check List Contains Another List ItemsOct 21
    • Python Program: 6 Ways to Generate Fibonacci SequenceOct 30
    • Python Program: Generate Fibonacci using RecursionOct 31
    • Python Program: Convert Lists into a DictionaryNov 1
    • Python Program: Insert Key-Value Pair in a DictionaryNov 13
    • Python Program to Find Sum of Two NumbersOct 9
    • Python Program: Swap Two Numbers Without a TempOct 10
    • Python Program: How to Sort Lists AlphabeticallyFeb 11
    • Python Program: How to Sort Dictionary by ValueFeb 10
    • Python Program: How to Sort a List of NumbersFeb 10
    View all →

    RELATED TUTORIALS

    generate random characters in python

    Python Random Character Generation

    By Meenakshi Agarwal
    5 months ago
    Generate random numbers list in Python

    Python Random Numbers List Generation

    By Meenakshi Agarwal
    5 months ago
    Convert Python list to string with examples

    Python Program: Convert a List to String

    By Meenakshi Agarwal
    1 year ago
    Learn how to do factorial in python

    Python Program to Find Factorial of a Number (7 Best Methods)

    By Harsh S.
    7 months ago
    © TechBeamers. All Rights Reserved.
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    • Terms of Use