Javascript random Numbers
Random Numbers
Because the Math.random() method returns values between zero and one, you must normalize its return value to fit the range of numbers required of your application. An easy way to get random integers in the range m to n (inclusive) is as follows:
Math.round(Math.random() * (n - m)) + m;
So to simulate a die roll you would use
roll = Math.round(Math.random() * (6 - 1)) + 1;
No comments :
Post a Comment