5120bee 5120bee - 1 year ago 109
Javascript Question

JavaScript: How To Code a Heads/Tails With Specific Probability Chance Percentage?

I've implemented the function:

function coinFlip() {
return(Math.floor(Math.random()*2) === 0) ? 'Heads' : 'Tails';

And it's all working fine (I already tested it).

My problem is, how do I make this function so that the probability of getting 'Heads' is 30% while the probability of getting 'Tails' is 70%?

Thanks in advance

Answer Source
function coinFlip() {
      return(Math.random() < 0.3) ? 'Heads' : 'Tails';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download