Percentage chance of saying something?

前端 未结 5 1125
自闭症患者
自闭症患者 2021-01-30 05:09

How do I make it so ..

  • 80% of the time it will say sendMessage(\"hi\");
  • 5 % of the time it will say sendMessage(\"bye\");
  • <
5条回答
  •  清酒与你
    2021-01-30 05:39

    Here is a very simple approximate solution to the problem. Sort an array of true/false values randomly and then pick the first item.

    This should give a 1 in 3 chance of being true..

    var a = [true, false, false]
    a.sort(function(){ return Math.random() >= 0.5 ? 1 : -1 })[0]
    

提交回复
热议问题