I am having some issue solving the Codercise H.1.2.. If I understood correctly the distribution of the output bit should be a Bernoulli distribution with p=0.5. The possible values for the output bit should be either 1 or 0.
res = np.random.choice([0,1])
return res
This is how I would write these instructions in python, however I get these errors:
Error: the secret rule should return a bit.
Error: local variable 'avg' referenced before assignment
If I am not mistaken, I am returning either 1 or 0 in my code. That’s why I don’t understand the first error message, and the second error message I am just guessing it is how the trials are calculated in the backend. It is probably a silly mistake but any help would be appreciated.
This is really weird . Your answer should be fine, but numpy does something really weird. Our solution is np.random.choice(2) as opposed to np.random.choice([0,1]) and we check to make sure that your answer returns an integer via isinstance(random_box(0), int). But:
So… hence the problem you’re seeing . I’ll make sure the check we do accounts for this slight type difference, but for now just use np.random.choice(2) so that you aren’t blocked
Thanks again for the quick answer. This is a very funny situation and I really thought I was going crazy. Anyways, thanks for letting me know why the error was happening.
input = 0 # MODIFY EXAMPLE
output = secret_box(input)
trials = 100 # INCREASE TRIALS TO IMPROVE APPROXIMATION
print(“On input”, input, “the approximate probability distribution is”, output)
We will secretly apply the function and return the result!
I am supposed to get a distribution, yet i only get a single value as an input even when i change the number of trials. Can you help me understand why ?