mardi 7 novembre 2017

How to use a random seed value in order to unittest a PRNG in Python?

I'm still pretty new to programming and just learning how to unittest. I need to test a function that returns a random value. I've so far found answers suggesting the use of a specific seed value so that the 'random' sequence is constant and can be compared. This is what I've got so far:

This is the function I want to test:

import random

def roll():
    '''Returns a random number in the range 1 to 6, inclusive.'''
    return random.randint(1, 6)

And this is my unittest:

class Tests(unittest.TestCase):

    def test_random_roll(self):
        random.seed(900)
        seq = random.randint(1, 6)
        self.assertEqual(roll(), seq)

How do I set the corresponding seed value for the PRNG in the function so that it can be tested without writing it into function itself? Or is this completely the wrong way to go about testing a random number generator?

Thanks




Aucun commentaire:

Enregistrer un commentaire