Coding Challenge 176: Buffons Needle @TheCodingTrain
Coding Challenge 176: Buffons Needle  @TheCodingTrain
Uploaded March 2023 | Updated September 2026, 1 week ago
How do toothpicks approximate the value of pi? I'll show you by coding a simulation in p5.js! Code: thecodingtrain.com/challenges/176-buffon-needle

πŸš€ Watch this video ad-free on Nebula nebula.tv/videos/codingtrain-coding-challenge-buffons-needle

p5.js Web Editor Sketches:
πŸ•ΉοΈ Buffon's Needle: editor.p5js.org/codingtrain/sketches/zqytzJAZN
πŸ•ΉοΈ Line Line Intersection: editor.p5js.org/codingtrain/sketches/htO0scRB1
πŸ•ΉοΈ Probability Density Function Graph: editor.p5js.org/codingtrain/sketches/eMaEGlfmu

πŸŽ₯ Previous video: youtu.be/PHfKCxjsmos?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
πŸŽ₯ All videos: youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH

References:
πŸ”— Buffon's Needle: en.wikipedia.org/wiki/Buffon%27s_needle_problem
πŸ”— Analysis and Simulation: https://mste.illinois.edu/activity/buffon/
πŸ”— Pickup Sticks & Pi: https://ogden.eu/pi/

Related Coding Challenges:
πŸš‚ 95 Approximating the Value of Pi: youtu.be/5cNnf_7e92Q
πŸš‚ 126 Toothpicks: youtu.be/-OL_sw2MiYw
πŸš‚ 145 Ray Casting 2D: youtu.be/TOEi6T2mtHo

Timestamps:
0:00 Happy Pi Day 2023!
0:45 Introduce Buffon's Needle
1:40 Unpack the math
4:40 Let toothpick fall at an angle
7:08 The probability that the toothpick crosses the border
7:27 Uniform probability density function
8:12 Illustrate the probability with a graph
10:49 Approximating the value of Pi
11:11 Calculating the area under the curve
12:48 Resources for Buffon's Needle
13:28 Start Coding
13:53 Draw vertical lines
14:20 Add a toothpick
15:02 Draw background in setup
15:57 We have to use the built-in value of Pi
17:00 Find the closest column
17:22 Determine if the toothpick intersects the column
18:10 Rotate the line relative to the y-axis
18:50 Draw the toothpick as a vertical line
19:40 Improve the visuals
20:48 Approximate the value of Pi using the probability that a toothpick intersects a column
23:45 Suggestions for variations
24:08 New Passenger Showcase page on the Coding Train website
24:38 Easter egg

Editing by Mathieu Blanchette
Animations by Jason Heglund
Pi Day Song by Will from America
Music from Epidemic Sound

πŸš‚ Website: thecodingtrain.com
πŸ‘Ύ Share Your Creation! thecodingtrain.com/guides/passenger-showcase-guide
🚩 Suggest Topics: github.com/CodingTrain/Suggestion-Box
πŸ’‘ GitHub: github.com/CodingTrain
πŸ’¬ Discord: thecodingtrain.com/discord
πŸ’– Membership: youtube.com/thecodingtrain/join
πŸ›’ Store: standard.tv/codingtrain
πŸ–‹οΈ Twitter: twitter.com/thecodingtrain
πŸ“Έ Instagram: instagram.com/the.coding.train

πŸŽ₯ Coding Challenges: youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
πŸŽ₯ Intro to Programming: youtube.com/playlist?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA

πŸ”— p5.js: p5js.org
πŸ”— p5.js Web Editor: editor.p5js.org
πŸ”— Processing: processing.org

πŸ“„ Code of Conduct: github.com/CodingTrain/Code-of-Conduct

This description was auto-generated. If you see a problem, please open an issue: github.com/CodingTrain/thecodingtrain.com/issues/new

#piday #pi #buffonsneedle #buffon #p5js #javascript
Coding Challenge 176: Buffons Needleml5.js: Training a Convolutional Neural Network for Image ClassificationCoding Challenge 164: Bending Time SlitscanSaturday Side Project Spooktacular!3.2 Angular Motion - Nature of CodeDiscord Bots 2: Discord Developer PortalCoding a Fractal Tree in 60 Seconds #programming #shortsReviewing the new Coding Train Passenger Showcase!Coding Train Live!Coding Train Live: Shaders and AutoencodersHow Fast Do Robots Think? #robot #coding #p5js #simulationIntroduction to Transformers.js (LIVE RECORDING SESSION ARCHIVE)
The Coding Train |

Coding Challenge 176: Buffon's Needle

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER