Lecture 18: Monte Carlo Part 2
Watch on YouTube →
Overview
Professor Rosen connects Monte Carlo simulation to practical programming projects, first outlining options such as Love2D, Pygame, Godot, web apps, and Blender, then estimating a card-game win rate with 100,000 trials. The main technical demonstration uses uniformly random points in a square to estimate pi from the fraction inside a unit circle, followed by pseudocode for validating and pronouncing Hawaiian words with indexed parsing and vowel dictionaries.
Key takeaways
- Monte Carlo simulation estimates the card game's win rate at about 42% by running 100,000 shuffled 3-by-3 deals and checking a selected line.
- For random points uniformly distributed over a square from -1 to 1 on each axis, the fraction inside a unit circle estimates pi/4; multiplying that fraction by 4 estimates pi.
- Python's random.uniform is appropriate for continuous coordinate sampling, while randint returns integers and would distort the geometric experiment.
- A project can be technically meaningful without commercial polish: a small game prototype, a useful Blender Python add-on, or a customized tutorial project can demonstrate learning.
- Hawaiian pronunciation is easier to implement with an indexed while loop than chained replacements or a character-only for loop, because double vowels require examining two characters and advancing by two.
Chapters
- Optional tracks in Canvas Module 11 provide tutorials for building projects from scratch.
- The Love2D track uses Lua to make Pong and Mario-style games; mobile-app tutorials cover Android and iOS.
- Static or dynamic websites can serve as portfolio pages, while a Flask tutorial builds a fake stock-trading site.
- Pygame lets students build games in Python, but Professor Rosen recommends discussing installation of the community edition early.
- Unreal Engine and Unity are industry-scale engines with extensive online tutorials and free use below stated revenue thresholds.
- Godot offers an open-source alternative with GDScript or C#, while Ren'Py suits visual novels.
- A freshman project need not be a polished commercial game; a moving character, collisions, explosions, or a monster-chase prototype can demonstrate learning.
- Past projects have included art, music, a Max/MSP synthesizer, and Blender add-ons written in Python.
- AI use is acceptable when disclosed, and Professor Rosen says human-made assets will receive consideration even when they are less polished.
- Professor Rosen describes building a RimWorld mod with XML and C#, which grew to nearly 200 factories.
- A puzzle-game project could clone a familiar game or add a twist, such as an adversarial Wordle variant.
- The Wordle-style program keeps a dictionary of possible words and returns feedback for the largest remaining group, delaying commitment to any one answer.
- The game lays nine shuffled cards in a 3-by-3 grid, with seven black cards and two red cards.
- A player wins by selecting any three-in-a-row line without revealing a red card.
- Because the player can choose among rows, columns, and diagonals, simple sequential probability calculations do not capture the full game.
- The setup function shuffles the nine-card list and slices it into three rows, returning a list of lists.
- A game trial checks whether the chosen line contains a red card; 100,000 trials estimate the win probability.
- The simulation gives a win rate of about 42%, and choosing a diagonal instead of a row does not materially change the estimate.
- The Runestone Monte Carlo exercise estimates pi by throwing random points into a square containing a unit circle.
- With radius 1, the circle's area is pi and the enclosing square has side length 2 and area 4.
- The expected fraction of points inside the circle is therefore pi/4, so multiplying the hit fraction by 4 estimates pi.
- The program imports Python's turtle and random modules so it can generate points and display them.
- Turtle world coordinates are set from (-1, -1) to (1, 1), placing the circle's center at (0, 0).
- A turtle named Bob moves to coordinate pairs; stamping records each sampled point on the canvas.
- Each throw independently generates x and y values between -1 and 1.
- Python's random.uniform is used for continuous real-valued samples; randint would produce integers and is unsuitable here.
- Bob's sampled positions visually fill the square as the number of throws increases.
- A point is inside the unit circle when its distance from (0, 0) is at most 1.
- The program computes distance as sqrt(x**2 + y**2), using math.sqrt, and returns a hit or miss.
- Hit points are marked blue and misses red, making the emerging circular boundary visible.
- Turtle's tracer settings reduce rendering overhead by updating only periodically, allowing thousands of samples to run faster.
- The program counts hits, divides by the number of trials, and multiplies by 4 to estimate pi.
- With 10,000 throws, the displayed estimate is about 3.18; more trials improve the estimate, but animation and drawing slow execution.
- The Hawaiian-word assignment is organized around separate validate and pronounce functions.
- Validation checks whether characters are permitted before pronunciation begins, allowing pronounce to assume its input is valid.
- Valid content includes Hawaiian letters and permitted spaces or apostrophes.
- String replacement can corrupt pronunciation output because replacement text may itself contain letters that later replacements target.
- A while loop with an index allows the program to inspect both the current character and the next one.
- The parser distinguishes double vowels, single vowels, and consonants; it advances the index by two for a double vowel and by one for a single character.
- A consonant list and dictionaries map vowel characters and vowel pairs to pronunciation guide sounds.
- The program checks whether the current and next characters form a double vowel by testing the pair as a dictionary key.
- Vowel and double-vowel cases append the pronunciation and a syllable-separating hyphen to the output.
Transcript excerpt
0:00 All right. So, might as well since we're talking about games, I guess. Okay. >> So, for those of you So, let's just So, this is going to follow a lot of last um last lecture what I did, which is that we're going to go over um I'm going to talk a bit more about final project options. is I'm going to talk a bit more about um and then I'm going to talk a bit more about these kind of weird Monte Carlo games that we can uh play. Um so and I think we'll have an exercise to do in Runstone, but let's move on to that. So first off, if you go to your Canvas page and you're one of those people who are like, I have no idea what I need to do for this final
0:47 project. I have no idea what I want to do. It's all too overwhelming. I don't know what choices to make, right? Um, I used to have these things as extra credit assignments. They are not extra credit assignments anymore, even even though they kind of sort of have like something associated with them with Dropboxes. That's just simply so I could uh propose them to you. So, I had all these tracks overview, right? Module 11. They're found under module 11. Um, and if you go down to these tracks, uh, like these are completely optional things to check out. Uh, basically they go over how uh, they go over like how you could build
1:35 something from scratch. They have videos on how to do it. So for if you're interested in learning uh game programming for instance, this game track teaches you how to build uh Pong and then Mario using the uh Lua Love 2D engine which was used for Bellatro. Um
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.