Random Number 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:

import unittest
import random

class CodingRoomsUnitTests(unittest.TestCase):

    def test_case1(self):
        import example
        random.seed(10)
        self.assertListEqual([93, 24, 74, 81, 93, 21, 46, 79, 82, 55], example.make_random_numbers())

    def test_case2(self):
        import example
        random.seed(55)
        self.assertListEqual([31, 45, 39, 58, 30, 43, 58, 31, 65, 80], example.make_random_numbers() )
    
    def test_case3(self):
        import example
        random.seed(2)
        self.assertListEqual([27, 31, 30, 66, 41, 59, 52, 97, 47, 97], example.make_random_numbers())

if __name__ == '__main__':
    unittest.main()

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 random

def make_random_numbers():
    numbers_list = []
    for num in range(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.

Grading Tests:

import unittest
import sys, io
import random

stdout = sys.stdout

class CodingRoomsUnitTests(unittest.TestCase):

    def setUp(self):
        global stdout
        stdout = sys.stdout
        sys.stdout = io.StringIO()

    def tearDown(self):
        global stdout
        sys.stdout = stdout
        
    def test_default_case(self):
        s = random.randint(1, 55)
        
        random.seed(s)
        import main
        output = sys.stdout.getvalue()

        random.seed(s)
        num = random.randint(0, 500)

        answer = str(num)
        student = output
        
        self.assertEqual(answer, student)

if __name__ == '__main__':
    unittest.main()

Sample Solution:

example.py
import random

random_num = random.randint(0,500)

print(random_num)

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:

import unittest
import sys, io
import random

stdout = sys.stdout

class CodingRoomsUnitTests(unittest.TestCase):

    def setUp(self):
        global stdout
        stdout = sys.stdout
        sys.stdout = io.StringIO()

    def tearDown(self):
        global stdout
        sys.stdout = stdout
        
    def test_default_case(self):
        s = random.randint(1, 55)
        
        random.seed(s)
        import example
        
        output = sys.stdout.getvalue().strip("\n")
        student = output

        random.seed(s)
        answer = random.choice(example.student_list)
        
        self.assertEqual(answer, student)

if __name__ == '__main__':
    unittest.main()

Sample Solution:

example.py
import random

student_list = ["first thing", "second thing", "third thing", "fourth thing"]

random_thing = random.choice(student_list)

print(random_thing)

Last updated