Skip to content

Instantly share code, notes, and snippets.

@rocktronica
Created March 9, 2012 22:37
Show Gist options
  • Save rocktronica/2009078 to your computer and use it in GitHub Desktop.
Save rocktronica/2009078 to your computer and use it in GitHub Desktop.
coinflip()
var results = { heads: 0, tails: 0, total: 0 };
function coinflip() {
var heads = !!Math.round(Math.random());
if (heads) {
results.heads++;
} else {
results.tails++;
};
results.total++;
console.log("Heads: " + Math.round(results.heads / results.total * 100) + "%; Tails: " + Math.round(results.tails / results.total * 100) + "%; Flips: " + results.total);
if (results.total >= 100) { clearInterval(flipping); }
}
var flipping = setInterval(coinflip, 25);
// will stop at 100 flips. to stop manually...
// clearInterval(flipping);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment