Playing with Math.random()


The first part of this page drew inspiration from the edx course Introduction to Computational Thinking and Data Science by MITx.

Monte Carlo Integration

At the end of lecture 8 the students were asked to write a code which would compute the value of the integral of sin(x) from 0 to Pi using Monte Carlo simulation.

Below is the graph showing this simulation. You can run it for a different amount of darts.

y = sin(x)

0 π





Result:

This is a result of only 1 trial. When integrated analytically the result is 2.



Probability

There's a fair share of coin tossing and dice rolling in the course. The textbook even explains the rules of craps and shows you the code to run the simulation.

Here's the histogram of simulation of rolling 2 dice n times.




# Probability Expected Actual
2 1 / 36
3 2 / 36
4 3 / 36
5 4 / 36
6 5 / 36
7 6 / 36
8 5 / 36
9 4 / 36
10 3 / 36
11 2 / 36
12 1 / 36



The Birthday Problem

I came across this article The birthday paradox on BBC website and it just didn't feel right. How could it be that in a group of 23 people there's 50% chance that 2 will share birthday? So here we run a simulation and the resulting graph is below. The article was correct.


The probability of a shared birthday exceeds 50% in a group of people.



Once in a Lifetime

There are 52! ways how to arrange a deck of 52 cards. This number is so big that it means that when you shuffle a deck of cards properly there's no chance you'd see the cards in the same order ever again. So enjoy the sight below - you'll never see it again.