For this piece of script:
var die = Math.floor(Math.random()*6 + 1);
It’s expected to generate a random number between 1 to 6.
The die number 6 before rounded floor is 6.0 to 6.999…999
However, the die number 1 before rounded floor is 1.00…001 to 1.99…999
Plus, because it is (random_nummber * 6) (“carry” in number system?)
Is it possible that number of generated in (1 to 1.9999) is different to (2 to 2.999)?
(possible the difference is 1)
Is this substantial/acceptable in real world? e.g. to fairly pick a customer for jack-pop. OR calculate possibility in gambling.
Or, did I do something wrong?
p.s. I’m not a math/science student, so I may miss a lot of math concepts.
The
Math.random()implementations in firefox and google chrome are pretty bad when it comes to uniform distribution (have not tested IE or others). You can easily roll your own that’s better.Here you can test them: https://web.archive.org/web/20150905080518/http://www.merlyn.demon.co.uk/js-randm.htm#TRFP
Here’s something to read: https://web.archive.org/web/20120502223108/http://baagoe.com/en/RandomMusings/javascript/
Here’s also a jsfiddle http://jsfiddle.net/mVrdE/2/ which runs 20 tests of 1 million coin flips for each method. Results for windows google chrome: