Birthday Paradox

https://en.wikipedia.org/wiki/Birthday_problem

by klenwell

HTML

<div class="banner">
  <p>For a <span id="chance">100</span>% chance you will find someone with your birthday, you will need to ask <span id="count">366</span> random people their birthday.</p>
</div>

CSS

body {
  background: #20262E;
  padding: 12px;
  font-family: Helvetica;
}

.banner {
  background: #fff;
  border-radius: 4px;
  padding: 16px;
  font-size: 14px;
  text-align: center;
  transition: all 0.2s;
  margin: 4px auto;
  width: 300px;
}

span {
  font-weight: bold;
  color: blue;
}

JavaScript

var COUNT = 5;

var $chance = $("span#chance");
var $count = $("span#count");

function probabilityOfMatch(n) {
	return 1 - probabilityAllDifferent(n);
}

function probabilityAllDifferent(n) {
	var probability = 1;
  
  for (var i=1; i<n; i++) {
  	var stepProb = 1 - (i / 365.0);
    probability = probability * stepProb;
  }
  
  return probability;
}

var chance = (probabilityOfMatch(COUNT) * 100).toFixed(2);

$chance.fadeOut(function() {
  $(this).text(chance).fadeIn();
});

$count.fadeOut(function() {
  $(this).text(COUNT).fadeIn();
});