Testing code that includes random numbers can be hard. Luckily, the random module comes with the seed() function.
The seed() function allows you to initialize the random number generator with a specific value , which forces it to produce the same values each time.
Example 1 - Student Function
In this example, students are supposed to create a function named make_random_numbers() which returns a list of 10 random numbers between 20 and 99 inclusive.
Notice that random.seed() is called, setting the randomly generated numbers to predetermined values, before you check if the functions return value equals the expected value.
Sample Solution:
example.py
import randomdefmake_random_numbers(): numbers_list = []for num inrange(10): numbers_list.append(random.randint(20,99))return numbers_list
Example 2 - Random Seed for random.randint()
Even better, we can randomly generate the seed and make the test random each time!
This example expects the student to use the randint() function to generate and print a random numbers between 0 and 500.