0
\$\begingroup\$

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);
});

jsfiddle here

If not, how could I randomize this even more to be fair enough to put a prize on the line.

\$\endgroup\$
  • 1
    \$\begingroup\$ Yes, Math.random should be fine for this \$\endgroup\$ – Jonah Jul 7 '17 at 18:02
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.