[removed] How To Code a Heads/Tails With Specific Probability Chance Percentage?

前端 未结 2 1957
情书的邮戳
情书的邮戳 2021-01-29 08:32

I\'ve implemented the function:

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

And it\'s al

相关标签:
2条回答
  • 2021-01-29 08:59

    If one of three toss coin is head it doesn't mean that in 10 toss, there will be 3 heads.. Here is your code with 500 toss (just change the number)

        function coinFlip() {
              return(Math.random() < 0.3) ? 'Heads' : 'Tails'; //ofc 0.3 is 30% (3/10)
        }
    
     var howManyTimes=500;
     var countHeads=0; 
     for (var i=0; i<howManyTimes;i++){
         if (coinFlip()==='Heads'){
           countHeads++;
         }
     }
     alert("Heads appear "+(countHeads/howManyTimes)*100+"% of the time");
    

    "how to solve a specific percentage problem"

    You can't, this is how probability works

    0 讨论(0)
  • 2021-01-29 09:08
    function coinFlip() {
          return(Math.random() < 0.3) ? 'Heads' : 'Tails';
     }
    
    0 讨论(0)
提交回复
热议问题