 # Python - Probability Calculator Challenge Issues

I’m quite likely not fully understanding something here, but I recently completed the last project for the Scientific Computing with Python Projects - The Probability Calculator.

One issue I found when completing this project is that the last test (which tests your experiment function) will only pass the test conditions when specific random methods are used in your hat function.

For example if you use the random.choice method on the hat contents to remove the balls, this gets you to the desired probability (~27%) in the number of trials (1000). However if you use a different random method i.e. random.shuffle the contents and then pop items from the contents, the resulting probability will not meet the self.assertAlmostEqual requirement (your result needs to be within 1% of 27.2%). If you instead increase the number of trials (e.g. 10000) then with more sampling you can get the random.shuffle selection method to pass.

I think this particular test might need a more lenient probability requirement or alternatively use a much larger sample size to work for different random methods? Or perhaps I am making some error here?

2 Likes

Hello there,

I have moved this over to the #contributors sub-forum, where improvements to the freeCodeCamp platform generally gets discussed.

Would you mind providing a link to some code to play around with?

Sure thing.

Here is my repl that passes the tests, using random.choice to select balls out the bag:

Here is the same repl but using random.shuffle and then popping the requisite number of balls from the hat contents (which should be an equivalent random selection of balls). This one fails the test unless you go to the tests and up the number of trials to e.g. 10k (or widen the margin of error accepted for the assertion).

I guess the answer in the test has come from using random.choice with 1000 trials, using the particular random seed (?). When other random methods are used with the same seed I guess they don’t converge on the answer (27%) as quickly and so fail the test unless the number of trials is increased.

I hope that makes sense and is helpful!

1 Like

I’m seeing this misunderstanding a lot. I’m not sure the best way to address it.

The pesudo random number generator needs to be seeded to a specific value for the test suite to pass, otherwise passing the test will be a pesudo random outcome.

In this case, you are defeating the purpose of setting the seed to a specific value by shuffling before you draw. If the seed was random, then your results will be pesudo random, then you won’t need to shuffle, so your code would work fine in production. If your seed isn’t random, then your output will be reproducible, by design.

So, don’t perterb the rng unless you need to.

2 Likes

Yes I understand your points. I suppose all I was trying to get across is that there is more than one possible way to ‘correctly’ write a random ball drawing method, however not all of these will pass the test suite as it stands.

Sure. That’s just the nature of the beast with rngs. We need some way to create predictability/reproducibility for testing.

I think file test_module.py is not coded perfectly has some logical bugs .This is my python code for probability calculator pls tell me where are I am wrong

``````def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
#number of expectatio completed
n = 0
ret_str = hat.draw(num_balls_drawn)
ret_dict = dict()

for i in range(num_experiments):
for j in range(len(ret_str)):
if(ret_str[j] in ret_dict):
ret_dict[ret_str[j]] +=1
else:
ret_dict[ret_str[j]] = 1
for key in expected_balls :
if(key in ret_dict):
if(expected_balls[key] == ret_dict[key]):
pass
else:
break
else:
break
n+=1
return n/num_experiments
``````
1 Like

I feel the issue has to do with the use of the `copy` module and not the use of the `random` module.
I also had issues with passing the last test of this project. But when I ran through your code, I saw that the only error I had in my code was not copying the hat object while executing the experiment function.
Using both `random.choice` and `random.randint` will produce the same result as long as you copy and use the copied hat object.