I was intrigued by the game often done ar parties, where they check to see if two people have the same birthday, so I wrote this app to do the calculation. I also added a calculation to give the probability that someone else would specifically have your birthday, which is much lower.
I cut off the calculation at a 90% match, but also made a color change when the probability exceeded 50%.
Here is a calculation at an early stage:
and at a later stage:
Notice that the specific match requires many more tries.
This is the code:
<!DOCTYPE html>
<html>
<head>
<title>Duplicate Birthdays</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }
</style>
</head>
<body>
<center> <input type=”text” id = “t1″ style=”color:white;background:black;font:normal normal 600 18px Arial” value = “” /> <select id=”s1″ size = “2” onclick=’strt();’><option>Any Match</option><option>Specific Match</option></select> <input type=”button” id=”b1″ value=”New” onclick=’nw();’ /></center>
<script>
var1 = 365; var2 = var1 – 1; var res = 1; var cnt = 0; var si; var ival;
function nw() {
clearInterval(si);
document.getElementById(“t1”).value = “”;
cnt = 0;
var1 = 365;
var2 = var1 – 1;
res = 1;
document.getElementById(“t1”).style.color = “white”;
}
function strt() {
if(document.getElementById(“s1”).value == “Any Match”) ival = 1000;
if(document.getElementById(“s1”).value == “Specific Match”) ival = 500;
si = setInterval(“calc()”, ival);
}
function calc() {
cnt ++;
res *= var2/var1;
document.getElementById(“t1″).value = cnt + ” ” + (100 * (1 – res)).toFixed(2) + “%”;
if (res <= .5) document.getElementById(“t1”).style.color = “#ffff66”;
if (res <= .1) {
clearInterval(si);
}
if(document.getElementById(“s1”).value == “Any Match”) var2 –;
}
</script>
</body></html>
I used the onclick property of a select element to start the calculation by calling strt():
function strt() {
if(document.getElementById(“s1”).value == “Any Match”) ival = 1000;
if(document.getElementById(“s1”).value == “Specific Match”) ival = 500;
si = setInterval(“calc()”, ival);
}
Since it takes many more tries to get a specific match, I chose a shorter interval for that.
The setInterval function calls calc():
function calc() {
cnt ++;
res *= var2/var1;
document.getElementById(“t1″).value = cnt + ” ” + (100 * (1 – res)).toFixed(2) + “%”;
if (res <= .5) document.getElementById(“t1”).style.color = “#ffff66”;
if (res <= .1) {
clearInterval(si);
}
if(document.getElementById(“s1”).value == “Any Match”) var2 –;
}
the variable cnt is the number of tries and is incremented each call:
cnt ++;
there are two variables var1 & var2 that determine the probability given by the variabler res:
res *= var2/var1;
The result is added to a text box and converted to a % probability:
document.getElementById(“t1″).value = cnt + ” ” + (100 * (1 – res)).toFixed(2) + “%”;
If res is less than .5 the color is changed to show that the probability is greater than 50%, and if .1 or less the calculation is stopped:
if (res <= .5) document.getElementById(“t1”).style.color = “#ffff66”;
if (res <= .1) {
clearInterval(si);
}
There is also a New button to reset for a new calculation:
clearInterval(si);
document.getElementById(“t1”).value = “”;
cnt = 0;
var1 = 365;
var2 = var1 – 1;
res = 1;
document.getElementById(“t1”).style.color = “white”;
}