Javascript random min max doesn't work -


i trying make simple program tabletop game calculates damage. want use random between 2 integers couple of percentage stuff calculate damage.

the problem random doesn't work me. doesn't matter numbers set min or max, starts 0 number before max.

<script> function showdiv() { var armor = document.getelementbyid('armortype').value; var damage = document.getelementbyid('dmgtype').value; var min = document.getelementbyid('mindmg').value; var max = document.getelementbyid('maxdmg').value;  document.getelementbyid('result').style.display = "block"; (var i=0;i<100;i++) {  var dmg_done = math.floor(math.random()*max+min+1); document.getelementbyid('test').innerhtml += " " + dmg_done; }  } </script> 

so min = 3, max = 6 following 100 numbers:

3 1 2 2 0 2 2 1 2 2 3 3 4 0 1 1 2 2 5 2 3 5 3 3 3 4 0 0 5 2 3 0 4 0 2 1 0 5 4 1 0 5 5 4 2 1 2 4 5 1 5 1 0 4 3 5 2 1 4 3 1 1 5 1 4 2 1 0 3 3 3 4 3 4 5 4 2 0 2 4 5 0 3 1 2 5 0 1 5 1 2 2 1 4 0 0 0 1 4 2

so doesn't matter min 3, randomizes 0 , there not single 6 in result.

demo:

http://jsfiddle.net/zprr6/

you want utilize such:

var dmg_done = math.floor(math.random() * (max - min + 1) + min); 

the reason starts @ 0, because math.random function produces float 0-1 (as many js functions , features do).

so, telling start @ max - min + 1, ie 4, avoids using 0 starting value.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -