function alg() {
var head_count = 0; // count for head
var tails_count = 0; // count for tails
var d = ""; // used to print to screen for troubleshooting
for (var i=0; i <= 1000; i++) {
var r = Math.random(); //random generator between 0 and 1
if (r < 0.5) {
//for head
head_count++;
}
else {
//for tails
tails_count++;
}
}
var head_P = (head_count/1000) * 100; //calculate percentage
var tail_P = (tails_count/1000) * 100; //calculate percentage
var p = ""; // string for printing to screen
p += "Head Probability: " + head_P + "%" + "</br>" + "Tails Probability: " + tail_P + "%";
//above is the probability for both head and tail
document.getElementById("print").innerHTML = p; // prints to screen
}
/*
function print() {
var p = "";
document.getElementById("print").innerHTML = p;
}
*/
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.