This tutorial explains several ways to generate a list of random numbers in Python. Here, we’ll mainly use three Python random number generation functions. These are random.randint(), random.randrange(), and random.sample().
Random Number List Generation in Python
You can find full details of these methods here: Python Random Number Tutorial. 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.
3 Simple Ways to Generate a List of Random Integers
Generating a random number is crucial for some applications. Let’s try to understand what can we do to achieve this with the help of simple examples.
Method: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.
Errors:
- It returns a ValueError for passing floating-point arguments.
- It returns a TypeError for passing any non-numeric arguments.
Example
""" 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]
Method:2 RandRange() to Generate a 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.
Errors:
It throws a ValueError when the stop value is smaller or equal to the start, or the input number is a non-integer.
Read more about, it here Python RandRange().
Example
""" 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]
Method:3 Sample() to Generate a 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 Tuple. Its purpose is random sampling with non-replacement.
Syntax:
random.sample(seq, k)
Parameters:
- seq: It could be a List, String, Set, or 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
""" 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]
Hopefully, you have now gained a good understanding of using different ways to generate a list of random numbers in Python. However, you may practice more with examples to gain confidence.
In case, you seek to learn more Python topics and concepts, read our step-by-step Python tutorial.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.