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().
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.