I know this may have been asked before, but I'm trying to have a Javascript function return one of three options. The problem is the chance for each option should be completely equal. This is what I have so far:
var choice = Math.random();
if (choice <= 0.34) {
choice = "option1";
} else if (choice <= 0.67) {
choice = "option2";
} else {
choice = "option3";
}
This is already pretty accurate, however the probability of "option3" is slightly lower. How can I reformulate this to have each option have the same chance of occuring? I would prefer a solution that doesn't involve using "0.3333333333333333..." in the if condition or something like that.
I think it would be cleaner and simpler to do something like this:
var options = ["option1", "option2", "option3"];
var choice = options[Math.floor(Math.random()*options.length)];