admin管理员组文章数量:1336008
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.
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.
Share Improve this question edited Jun 13, 2012 at 6:40 Edditoria asked Jun 13, 2012 at 6:24 EdditoriaEdditoria 1991 silver badge15 bronze badges 3- 1 You can always roll your dice a few million times and count the occurence of each value to find out. What distribution do you need? I expect it will be sufficiently random for gambling, but not for cryptography. – RobG Commented Jun 13, 2012 at 6:33
- 2 A couple of months ago I got into an argument with a friend about the same subject, after many tests we arrived to the conclusion that the results of Math.random() are pretty much evenly distributed. Check this JSFiddle – Adi Commented Jun 13, 2012 at 6:36
- To readers visiting here: the best answer is chosen because the knowledge "why". You will be interested to read all other answers. They are very very good to read deeply, especially "how" to proof, etc. Thank you to all of people help here :) – Edditoria Commented Jun 13, 2012 at 15:29
6 Answers
Reset to default 3Here's a simple test:
<div id="msg"></div>
<script type="text/javascript">
function rollDice() {
return Math.random()*6 + 1 | 0;
}
function testDice() {
var results = [,0,0,0,0,0,0];
var i = 100000;
var n;
while (i--) {
results[rollDice()] += 1;
}
return results;
}
document.getElementById('msg').innerHTML = testDice();
</script>
In IE I get: ,16768,16783,16546,16862,16447,16594
. That looks fairly even to me but you may need to run it several times and pare results.
Math.random
is a pseudo random number generator. It doesn't generate truly random numbers. However it's perfectly acceptable to be used for rolling die. See the following JS fiddle:
var roll = random.bind(null, 6); // Generate a random number from 1 to 6.
function keepRolling(times) {
var die = [0, 0, 0, 0, 0, 0]; // How many times each number is rolled.
for (var i = 0; i < times; i++) die[roll() - 1]++;
return die;
}
var die = keepRolling(10000); // Roll the die 10000 times.
for (var i = 0; i < 6; i++) {
alert("The number " + (i + 1) + " was rolled " + (die[i] / 100) + "% of the time.");
}
function random(range) {
return Math.ceil(range * Math.random());
}
From what I see Math.random
is sufficiently random for your purpose.
The Math.random()
implementations in firefox and google chrome are pretty bad when it es 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/web/20150905080518/http://www.merlyn.demon.co.uk/js-randm.htm#TRFP
Here's something to read: https://web.archive/web/20120502223108/http://baagoe./en/RandomMusings/javascript/
Here's also a jsfiddle http://jsfiddle/mVrdE/2/ which runs 20 tests of 1 million coin flips for each method. Results for windows google chrome:
Math.random() min bias: 2 max bias: 1270
rand() min bias: 2 max bias: 15
Try code below
var die = Math.floor((Math.random() * 10000 % 6) + 1);
Try to change the mod value to a large value. Multiplying it with large numbers and mod it to 6, would give large probability space before narrowing it down to your specific range (that is 0 - 5)
Just give it a try and see if the result is acceptable.
The reason why Math.ceil()
is not correct is because zero is a possible return value to the Math.random()
function. Rounding up from zero is still zero. Zero is obviously not a valid die roll.
Nevertheless, it is a highly unlikely result considering that the return value has 16 decimal points.
本文标签: javascriptIs rolladice script evenly distributed(with Mathrandom())Stack Overflow
版权声明:本文标题:javascript - Is roll-a-dice script evenly distributed ? (with Math.random()) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801057a2398226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论