import random # Sock Simulation def sock_sim(num_trials): num_matches = 0 for trial in range(num_trials): socks = ['red', 'red', 'green', 'green', 'blue', 'blue'] ## # Draw first sock ## sock1 = random.choice(socks) ## ## # Remove first sock ## socks.remove(sock1) ## ## #Draw second sock ## sock2 = random.choice(socks) # Is there another way to do this? sock_choice = random.sample(socks, 2) sock1 = sock_choice[0] sock2 = sock_choice[1] if sock1 == sock2: num_matches += 1 return num_matches/float(num_trials) # Disease Simulation def disease_sim(num_trials): num_survivors = 0 for trial in range(num_trials): pills_needed = {} tickets = [] pills_gotten = {} for person in range(100): # Randomly determine how sick each person is pills_needed[person] = random.randint(1, 5) # Add tickets to pile tickets.extend([person, person, person, person, person]) # Initialize as having 0 pills pills_gotten[person] = 0 # Draw 320 tickets drawn_tickets = random.sample(tickets, 320) # Determine how many pills each person has gotten for ticket in drawn_tickets: pills_gotten[ticket] += 1 # Add the survivors from this trial for person in pills_gotten: if pills_gotten[person] >= pills_needed[person]: num_survivors += 1 # Return average number of survivors return num_survivors/float(num_trials)