Probability Calculator unit test issue/question

 def test_prob_experiment(self):
        hat = prob_calculator.Hat(blue=3,red=2,green=6)
        probability = prob_calculator.experiment(hat=hat, expected_balls={"blue":2,"green":1}, num_balls_drawn=4, num_experiments=1000)
        actual = probability
        expected = 0.272
        self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')

So, I’m not worried about getting someone to help me with my actual code. I need help understanding why probability changes, like it’s supposed to, running it, but once the unit test runs it I get one answer and it never changes.

Reading over the test, deleting my code and rewriting it, and researching why in this scenario the actual and expected doesn’t change has driven me nuts. So far, I’ve not had to reach out to anyone for help with my code and I’ve realized most of my issues are usually pretty easy to solve once I make sure I understand 100% the rules and have a good understanding how the unittest is working. Now I’m stuck cause this unit test I’m puzzled by.

Any help or clarity is greatly appreciated.

Looks like it might be something to do with the random.seed() before the unit tests start. I’m going to look into the random.seed() to see how it works and is used elsewhere and maybe I can find out my issue.

It is to do with random. You can look at another post of mine on a similar issue for more information. In this particular situation, the unit test sets the seed of the random number generator for every test so that it will generate the same sequence of random numbers every time the tests are run, which generates the same probability every time, thus simplifying the test construction. You can seed your generator too, and cause the same behavior.

The probability changes from run to run (unless you seed the generator) because you are simulating the situation with a small number of trials, which will generate a final probability (pseudo-?) randomly distributed around the actual probability, which can be worked out analytically. If you let your program run for many iterations, it should converge on the analytical probability.


Ahhhh, thank you! That helps a ton, especially, I didn’t know I could seed the generator. I will definitely give this a shot.

Not to blow your alerts up, but thank you a ton! I played around with it now after taking what you said and reading a little more documentation and examples of the random.seed(), and, also, noticed some miscalculations in my code. I now have to clean it all up and make it better, but I know it would’ve took me longer to figure this out if you hadn’t helped. You pointed me in the right direction, thank you.

1 Like