Hook

Their other posts in the index, biggest breakout first.
Monte Carlo simulation is a technique where you estimate an answer by running a system with random inputs many times and measuring how often an outcome occurs. def run_simulation(args: dict) -> list[int]: # write me return 42 def is_success(result: list[int]) -> bool: # write me return 42 def monte_carlo(num_trials: int = 100000, args: dict = {}) -> float: # the monte carlo method template successes = 0 for _ in range(num_trials): results = run_simulation(args) if is_success(results): successes = 1 return successes / num_trials def monte_carlo_birthday_problem(n: int, days_in_year: int = 365) -> float: """Use Monte Carlo simulation to estimate the probability that at least two people share the same birthday in a group of n people.""" Args: n: Number of people in the group days_in_year: Number of days in a year (default 365) Returns: Estimated probability (between 0 and 1) """ return monte_carlo(args={'num_people': n, 'days_in_year': days_in_year}) There are 25 people in a room. What's the probability that two of them have the same birthday? I don't know, let's write a program to figure it out. Hi, I'm Tara. I'm a senior software engineer and I used to teach computer science at Carnegie Mellon. This is my intro to the Monte Carlo method. Monte Carlo simulation is a technique where you estimate an answer by running a system with random inputs many times and measuring how often an outcome occurs. So I've kind of started us off here with a template and basically we're gonna keep track of the successes that we have and then we'll run this loop so basically we're gonna run it for the number of trials and we've set this to be 100,000 trials by default. So we're gonna run this simulation a hundred thousand times basically and just see what happens. And so we're gonna run the simulation, we'll get the results, we'll pass the results into this is success function. If it's a success, we will increment the successes variable and at the very end, we'll count how many trials were successes out of the total number of trials. So this template will work for any problem in which there's a clear binary success and failure case. And so basically in the birthday problem, we run this Monte Carlo where we have two arguments. One of them is the number of people and the other one is the number of days in the year. So let's actually write these simulations. So for my n people, I'm just gonna pull that out of args really quick. Okay, basically I need to generate a birthday for each of my people in my room. So I'm gonna just do this as a list comprehension. I'm gonna say for person in range of n and then I'm gonna use the random module to do this. So I'm gonna say random.randint and it should go from I mean, we can just do one to the number of days in the year. Is it a success? Well, we generate our birthdays. So maybe we have a list of like 1, 3, 60, 50, whatever. And so, how do we know if two people have the same birthday? Well, that would be if we had like two 50s in the list of birthdays, right? And so a really neat way to do this is for our results list here, we would basically say is the length of the result list not equal to the length of the set of the result list. And the set here, so whenever you have a set that basically removes duplicates. And so if we did have any duplicates in our result list, meaning we had two people with the same birthday or at least two people with the same birthday, then this length would be smaller than the length of the whole result list because it would remove the duplicates. So that's a nifty little trick you can use to detect duplicates. And that's pretty much it. That's all the code we need to write here. The Monte Carlo template takes care of the rest. So let's print out what it is. Print the Monte Carlo birthday problem and I want to have 25 people and then we already have this default set here. Okay, it's about a 56% chance, which is actually really high. So finally, I am actually feeling a little curious about what this would look like visualized and I don't feel like writing that myself. So I'm gonna show you how I use AI to write things for myself sometimes. So I'm gonna say write a function that visualizes a chart where the x axis is n for a number of people and the y axis is the probability. I like to watch what it's doing as it's going because I don't always trust it. Yeah, this checks out. Okay, so here's our graph and I feel like this makes sense because in the beginning when you have very few people, it's unlikely anyone will share the same birthday. And you kind of hit this stride in the middle. Uh, you hit 50% really quickly. 50% is like right here and I'm pretty sure the magic number is 23 people gives you a 50% chance. Um, and then as we approach the end, when you have like, I mean, even over 60 people, it's almost impossible for no one to share a birthday. And I feel like that number seems smaller than you would think, which is why this problem is cool. Okay, thanks for learning along with me.