The JavaScript function Math.random() randomly generates a number from 0 to slightly less than 1 (shown as <1).
Generate random numbers from 0 to <3
Math.random() * 3
This gets us numbers from 1 to <3
Math.random() * 2 + 1
Math.random() is explained more here.
Use Math.floor() to round numbers downward to the nearest integer:
Math.floor(Math.random()*3+1)
will get a whole number between 1 and 3
To generate random numbers between 1 and max —
Math.floor(Math.random()*max+1)
This article discusses how to generate a random negative number or a random number between a range including both negative and positive numbers. Here is the basic rule:
For negative values from -9 to 0 subtract 9 from the previous result:
var negativeRandomN = Math.floor(Math.random()*10) – 9
To generate a random integer from m to n:
var randomN = Math.floor(Math.random() * (n-m+1)) + m