Still travelling and tryna understand some python internals. But I've got about 2 hours in airport and I'm gonna write about Monte-Carlo Simulations.
More math guys, but useful math.
Instead of cooking up formulae, we'll sample a bunch of random values and estimate a good approximation iteratively.
Why?
Because some problems are just too hard to solve analytically. There's not gonna be any straightforward formula for a problem, or the problem itself is too complicated like simulating light in a highly dense, reflective, refractive scene with complex geometry. You can't write a neat equation for that.
Instead, you
1. Model the process stochastically.
2. Use random sampling.
3. Estimate results by averaging iteratively.
Let's assume we don't know the value of \( \pi \). Here's how we can estimate an approximate value of it.
let pi = 0
let insideCircle = 0
let totalPoints = 1000000
for (let i = 0; i < totalPoints; i++) {
let x = Math.random()
let y = Math.random()
if (x*x + y*y <= 1) {
insideCircle += 1
}
}
pi = 4 * (insideCircle / totalPoints)
console.log("Approximate pi =", pi)
Click run
Here's a fun visualisation that estimates the area of the batman logo.
You can always reach out to me on twitter - @pwnfunction.