I have an array of 4 names, and was wondering what the best best way to reliably pick 1 name for a contest would be. I've read online that javascripts Math.Random
is only pseudo random
. Which makes me wonder, would this be a fair enough function to put a prize on the line?
The picking process won't be externally/customer facing, so I'm not worried about how it looks, more just about how fair it would be.
$(".randomize").click(function() {
var contestents = [{
person: "Kareem"
}, {
person: "Charlie"
}, {
person: "Hobie"
}, {
person: "Hoder"
}];
var winner = Math.floor(Math.random() * contestents.length)
$('div').html(contestents[winner].person);
});
If not, how could I randomize this even more to be fair enough to put a prize on the line.
-
1\$\begingroup\$ Yes, Math.random should be fine for this \$\endgroup\$Jonah– Jonah2017年07月07日 18:02:26 +00:00Commented Jul 7, 2017 at 18:02
1 Answer 1
You are correct. Math.random
is indeed pseudo-random. However, its algorithm is actually quite good. In this SO post, a concern is brought up that the randomly generated numbers tend to all have the same number of digits. While this was statistically proven to make sense, you won't run into a similar problem since you are only dealing with single digit numbers.
Math.random
should be random enough for your purposes. However, if you want to get truly random numbers to be absolutely fair, check out RANDOM.ORG. This, however, would obviously be a lot more unwieldy than just using Math.random.