Problems with Codercise H.1.2

Hi,

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.

Cheers,
Erick

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:

2 Likes

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.

Cheers,
Erick

1 Like

Hello
In this part of the code

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 ?

Thanks

Hello @yoshypdf

This does seem to be a problem with the Codebook grader. I’ll look into it and let you know once it’s fixed. Thanks for your patience!

Alvaro