Probability calculator

Hi, I have a problem with my project. I don’t understand what I fall into the function of the experiment.
The probability is not it.
Questo è il mio codice:

Could you explain how this part of the experiment function is supposed to work?

  for contare in range(num_experiments):
      lista_copia=lista_vuota.copy()
      estrazione=random.choices(lista_copia,k=num_balls_drawn)
      palline_verdi=estrazione.count('green')
      palline_rosse=estrazione.count('red')
      if palline_verdi==1 and palline_rosse==2:
         count=count+1
1 Like

In that function I’m practically making a copy of the hat so as not to overwrite it during the numerous draws that are 2000 and then I’m randomly choosing the balls from that hat (which is the cappello_extraction variable that being a method I extracted through the Hat builder’s class and then I inserted the balls in the list called empty_list, although I also inserted the balls of the previous hat contained in the Hat class but I didn’t know how to do otherwise maybe it’s wrong).After the extraction that always causally chooses five balls then I count the number of times that the green and the red ones appear and if they correspond to 2 and 1 increase count or the count of the times when this combination occurs in the experiment and then at the end I calculate the whole probability by dividing the number of times that special combination appears and the number of experiments and then print but the value of the probability does not correspond to the one present in the test. I don’t know, maybe I’m wrong. Thanks you

What if number or colors of expected balls is different than green: 1, red: 2?

1 Like

Maybe I should add an else and a continuum. I don’t know.

Okay, but what if after adding another set of expected balls, somebody would want to use yet different expected balls?

Try to think about a way that would not require manually adding the variations of expected balls, but would use expected_balls that are passed to the function, to determine if drawn balls fulfill the requirements.

Ok I changed the code and I put the fact that I have to calculate two different probabilities in case the balls were like the ones they expect or were others but the same the value is not that.

count=0
conto=0
for contare in range(num_experiments):
lista_copia=lista_vuota.copy()
estrazione=random.choices(lista_copia,k=num_balls_drawn)
elenco=
for t,r in expected_balls.items():
f=[t]*r
elenco.extend(f)
for tt in elenco:
if tt in lista_copia:
count=count+1
probabilità=count/num_experiments
return probabilità
else:
probabilità_2=conto/num_experiments
return probabilità_2

There’s some complexity that makes it a bit hard to understand the logic. There should be just one probability, but returned only after all experiments from the number of num_experiments are run.

Try taking a step back and define again how single experiment from the number of num_experiments should look like.