Tell us what’s happening:
I need help.
Not sure if I’m using the correct python random function samples. Tried choices and randonint and still cannot match the probability.
I print debug statements to print out the returned ball list. I grepd the results to verify the result test match the expected list.
Can someone look at the code to see were I’m going wrong?
Your code so far
import copy
import random
import secrets
class Hat():
def __init__(self,**args):
#if your hat is {'red': 2, 'blue': 1}, contents should be ['red', 'red', 'blue']
self.args = args
self.contents =[]
for x,y in args.items():
self.contents.extend([x] * y)
self.randon_limit = len(self.contents)
def draw(self, number_of_balls):
#If the number of balls to draw exceeds the available quantity, return all the balls.
if number_of_balls > len(self.contents):
copy_contents = self.contents.copy()
self.contents = []
print( ".>= return ")
return copy_contents
returned_balls = []
contents = self.contents #.copy()
# random.sample() is an built-in function of random module in Python that
#returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.
# drawn_balls = []
# while len(drawn_balls) != number_of_balls:
# ball = random.choice(self.contents)
## self.contents.remove(ball)
## drawn_balls.append(ball)
# drawn_balls = []
# while len(drawn_balls) != number_of_balls:
# ball = secrets.choice(self.contents)
# self.contents.remove(ball)
# drawn_balls.append(ball)
drawn_balls = random.sample(self.contents,number_of_balls)
for ball in drawn_balls:
self.contents.remove(ball)
#print(f'drawn_ball: {drawn_balls}\n contents {self.contents}')
return drawn_balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
n = 0
m = int(num_experiments)
print(f'////////////////////////////////////// \n start {hat.args} expected: {expected_balls}, num: {num_balls_drawn}, iterations:{num_experiments}')
print(f'\n////////////////////////////////////// ')
for _ in range(num_experiments):
# Create a copy of the hat to avoid altering the original
hat_copy = copy.deepcopy(hat)
ret_list = hat_copy.draw(num_balls_drawn)
returned_balls = {x:ret_list.count(x) for x in ret_list}
#print(f'**returned {returned_balls}')
hit = True
#if len(returned_balls) == len(expected_balls):
for color, number in expected_balls.items() :
if returned_balls.get(color, -1) != number:
hit = False
break
print(f' { hit}: = returned: {returned_balls}')
if hit:
# print(f' HIT from call: = expected: {expected_balls}')
n += 1
if hit:
n += 1
probability = n / num_experiments
print('&**$$$\n', n, '/' , num_experiments, '=', probability, '\n&**$$$\n' )
return probability
hat = Hat(black = 6, red = 4, green = 3, blue = 2)
probability = experiment(hat,
expected_balls= {'red': 2, 'green': 1},
num_balls_drawn=5,
num_experiments=20)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project