TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Generate a List of Random Integers in Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Generate a List of Random Integers in Python

Last updated: Feb 04, 2024 12:36 am
By Meenakshi Agarwal
Share
5 Min Read
Generate random numbers list in Python
Generate random numbers list in Python
SHARE

This tutorial explains several ways to generate random numbers list in Python. Here, we’ll mainly use three Python random number generation functions. These are random.randint(), random.randrange(), and random.sample().

Contents
1. randint() to Generate List of IntegersExample-2. randrange() to Generate List of NumbersExample-3. sample() to Generate List of IntegersExample-

You can find full details of these methods here: Python random number generator. All these functions are part of the Random module. It employs a fast pseudorandom number generator which uses the Mersenne Twister algorithm.

However today, we’ll focus on producing a list of non-repeating integers only. Go through the below bullets to continue.

1. randint() to Generate List of Integers
2. randrange() to Generate List of Integers
3. sample() to Generate List of Integers

Generating a random number is crucial for some applications and possessing many usages. Let’s try to understand each of these functions with the help of simple examples.

Generate a List of Random Integers

1. randint() to Generate List of Integers

It is a built-in method of the random module. Read about it below.

Syntax:

random.randint(Start, Stop)

Arguments:

(Start, Stop) : Both of these should be integers.

Return value:

It returns a random integer value in the given range.

Error status:

  • It returns a ValueError for passing floating-point arguments.
  • It returns a TypeError for passing any non-numeric arguments.

Example-

Source Code

"""
 Desc:
  Generate a list of 10 random integers using randint()
"""

import random

Start = 9
Stop = 99
limit = 10

RandomListOfIntegers = [random.randint(Start, Stop) for iter in range(limit)]

print(RandomListOfIntegers)

Output

[35, 86, 97, 65, 86, 53, 94, 15, 64, 74]

2. randrange() to Generate List of Numbers

It produces random numbers from a given range. Besides, it lets us specify the steps.

Syntax:

random.randrange([Start,] Stop[, Step])

Arguments:

  • Start: Generation begins from this number. It’s an optional parameter with zero as the default value.
  • Stop: Generation stops below this value. It is a mandatory parameter.
  • Step: It is value to be added in a number. It is also optional, and the default is 1.

Return value:

It returns a sequence of numbers beginning from start to stop while hopping the step value.

Error status:

It throws a ValueError when the stop value is smaller or equals to the start, or the input number is a non-integer.

Read more about, here Python randrange().

Example-

Source Code

"""
 Desc:
  Generate a list of 10 random integers using randrange()
"""

import random

Start = 9
Stop = 99
limit = 10

# List of random integers without Step parameter
RandomI_ListOfIntegers = [random.randrange(Start, Stop) for iter in range(limit)]
print(RandomI_ListOfIntegers)

Step = 2
# List of random integers with Step parameter
RandomII_ListOfIntegers = [random.randrange(Start, Stop, Step) for iter in range(limit)]
print(RandomII_ListOfIntegers)

Output

[52, 65, 26, 58, 84, 33, 37, 38, 85, 82]
[59, 29, 85, 29, 41, 85, 55, 59, 31, 57]

3. sample() to Generate List of Integers

It is a built-in function of Python’s random module. It returns a list of items of a given length which it randomly selects from a sequence such as a List, String, Set, or a Tuple. Its purpose is random sampling with non-replacement.

Syntax:

random.sample(seq, k)

Parameters:

  • seq: It could be a List, String, Set, or a Tuple.
  • k: It is an integer value that represents the size of a sample.

Return value:

It returns a subsequence of k no. of items randomly picked from the main list.

Example-

Source Code

"""
 Desc:
  Generate a list of 10 random integers using sample()
"""

import random

Start = 9
Stop = 99
limit = 10

# List of random integers chosen from a range
Random_ListOfIntegers = random.sample(range(Start, Stop), limit)
print(Random_ListOfIntegers)

Output

[97, 64, 82, 85, 96, 93, 76, 62, 36, 34]

We hope that after wrapping up this tutorial, you should feel comfortable to generate random numbers list in Python. However, you may practice more with examples to gain confidence.

Also, to learn Python from scratch to depth, do read our step by step Python tutorial.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

TAGGED:Random Data Generation Made Easy
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python String Find with Examples Your Ultimate Guide to Python String Find()
Next Article MySQL TIMESTAMP with Simple Examples MySQL TIMESTAMP with Examples

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x