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.

asked Jul 7, 2017 at 17:17
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Yes, Math.random should be fine for this \$\endgroup\$ Commented Jul 7, 2017 at 18:02

1 Answer 1

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.

answered Jul 7, 2017 at 20:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.