// Flip a Coin // define the range of numbers to pick from var lowest = 1; // lowest possible side of the coin // 1 for heads var highest = 2; // highest possible side of the coin // 2 for tails var number_of_coins = 1; // how many coins to flip // allow the coin to land on the side // change to false if sidelanding not desired var allow_sidelanding = true; var PROBABILITY_OF_SIDELANDING = 6000; // set the probability of sidelanding to 1 in 6000 var this_flip = []; // array to store the results of this flip for (var j = 1; j <= number_of_coins; j++) { // loop for the number of coins // for each coin, generate a number between lowest and highest i.e. 1 or 2 var coin_face = Math.floor(Math.random() * (highest-lowest+1) + lowest); // if sidelanding is allowed if (allow_sidelanding) { // first check for sidelanding scenario with given probability var edge = Math.floor(Math.random() * (PROBABILITY_OF_SIDELANDING-1+1) + 1); // if sidelanding is probable, override the coin face with edge value i.e. 3 // i.e. if we generated a 1 when generating a number between 1 and PROBABILITY_OF_SIDELANDING if (edge === 1) { coin_face = 3; } } this_flip.push(coin_face); //store this in the array } // print all the generated flips for (j = 0; j < this_flip.length; j++) { // loop through the coin array //print each coin flip value followed by a space document.write(this_flip[j]); document.write(" "); } /* Sample output */