Random Numbers Tests
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.

Grading Tests:

1
import unittest
2
import random
3
​
4
class CodingRoomsUnitTests(unittest.TestCase):
5
​
6
def test_case1(self):
7
import example
8
random.seed(10)
9
self.assertListEqual([93, 24, 74, 81, 93, 21, 46, 79, 82, 55], example.make_random_numbers())
10
​
11
def test_case2(self):
12
import example
13
random.seed(55)
14
self.assertListEqual([31, 45, 39, 58, 30, 43, 58, 31, 65, 80], example.make_random_numbers() )
15
16
def test_case3(self):
17
import example
18
random.seed(2)
19
self.assertListEqual([27, 31, 30, 66, 41, 59, 52, 97, 47, 97], example.make_random_numbers())
20
​
21
if __name__ == '__main__':
22
unittest.main()
Copied!
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
1
import random
2
​
3
def make_random_numbers():
4
numbers_list = []
5
for num in range(10):
6
numbers_list.append(random.randint(20,99))
7
return numbers_list
Copied!

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.
Grading Tests:
1
import unittest
2
import sys, io
3
import random
4
​
5
stdout = sys.stdout
6
​
7
class CodingRoomsUnitTests(unittest.TestCase):
8
​
9
def setUp(self):
10
global stdout
11
stdout = sys.stdout
12
sys.stdout = io.StringIO()
13
​
14
def tearDown(self):
15
global stdout
16
sys.stdout = stdout
17
18
def test_default_case(self):
19
s = random.randint(1, 55)
20
21
random.seed(s)
22
import main
23
output = sys.stdout.getvalue()
24
​
25
random.seed(s)
26
num = random.randint(0, 500)
27
​
28
answer = str(num)
29
student = output
30
31
self.assertEqual(answer, student)
32
​
33
if __name__ == '__main__':
34
unittest.main()
Copied!
Sample Solution:
example.py
1
import random
2
​
3
random_num = random.randint(0,500)
4
​
5
print(random_num)
Copied!

Example 3 - Random Seed for random.choice()

Here is another where the student needed to print a random item from a list they created named student_list.
Grading Tests:
1
import unittest
2
import sys, io
3
import random
4
​
5
stdout = sys.stdout
6
​
7
class CodingRoomsUnitTests(unittest.TestCase):
8
​
9
def setUp(self):
10
global stdout
11
stdout = sys.stdout
12
sys.stdout = io.StringIO()
13
​
14
def tearDown(self):
15
global stdout
16
sys.stdout = stdout
17
18
def test_default_case(self):
19
s = random.randint(1, 55)
20
21
random.seed(s)
22
import example
23
24
output = sys.stdout.getvalue().strip("\n")
25
student = output
26
​
27
random.seed(s)
28
answer = random.choice(example.student_list)
29
30
self.assertEqual(answer, student)
31
​
32
if __name__ == '__main__':
33
unittest.main()
Copied!
Sample Solution:
example.py
1
import random
2
​
3
student_list = ["first thing", "second thing", "third thing", "fourth thing"]
4
​
5
random_thing = random.choice(student_list)
6
​
7
print(random_thing)
Copied!
​
Last modified 2mo ago