Percentage chance of saying something?

前端 未结 5 1131
自闭症患者
自闭症患者 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:54

    For cases like this it is usually best to generate one random number and select the case based on that single number, like so:

    int foo = Math.random() * 100;
    if (foo < 80) // 0-79
        sendMessage("hi");
    else if (foo < 85) // 80-84
        sendMessage("bye");
    else // 85-99
        sendMessage("test");
    

提交回复
热议问题