Problems with Codercise H.1.2


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.


1 Like

Hey @erqs!

This is really weird :thinking:. 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:

>>> type(np.random.choice(2))
<class 'int'>
>>> type(np.random.choice([0,1]))
<class 'numpy.int64'>

So… hence the problem you’re seeing :sweat_smile:. 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 :slight_smile:


Hey @isaacdevlugt!

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.


1 Like