DEV Community

Cover image for 4 Full Examples of Monte Carlo Simulation
Isaac Lee
Isaac Lee

Posted on • Updated on • Originally published at

4 Full Examples of Monte Carlo Simulation

People land (stumble) on my blog the most to learn Monte Carlo simulation. It's a numerical method for solving real-world problems, especially those for which we don't know the exact solution. It's also a great way to learn coding and use math to further understand a problem.

Let me show you 4 examples, best to be read in sequence. The codes on my blog are in Matlab, but you can also find them in Python and R on GitHub.

1. Craps

Skills covered:

  • Random number generation
  • If-else statement
  • While and for loops
  • Conditional probability

Dice being rolled

2. Penney's Game

Skills covered:

  • Arrays
  • String operations
  • User-defined function
  • Conway's algorithm

Probability distribution of winning Penney's game

3. Buffon's Needle

Skills covered:

  • Ceiling and floor functions
  • Min and max functions
  • Trigonometry & calculus
  • Affine transformation
  • Vectorization (parallelization)
  • Polymorphism

Needles intersecting lines

4. How Big Is Your Heart?

Skills covered:

  • Dimensional analysis
  • Boundary value analysis
  • Size-dependent simulation

Hearts of various sizes

Top comments (2)

mxl profile image
Maria Boldyreva

Thank you for the articles, they are very informative, yet simple.
The second link leads to the first post, btw.

ijlee2 profile image
Isaac Lee

Hi, Maria. I'm glad that you found them helpful. Thanks for letting me know about the incorrect link, it's fixed now. :)