Possible Duplicate:
Generating random numbers in Javascript
How do I get a random number between −10 and 10 in JavaScript?
Possible Duplicate:
Generating random numbers in Javascript
How do I get a random number between −10 and 10 in JavaScript?
var min = -10;
var max = 10;
// and the formula is:
var random = Math.floor(Math.random() * (max - min + 1)) + min;
jQuery: $.randomBetween(minValue, maxValue);
Using not more than a simple Google search,
var randomnumber=Math.floor(Math.random()*21)-10
Math.random()*21)
returns a number between 0 and 20. Substracting 10 would yield a random number between -10 and 10.
My advice: try to Google first, and than ask about the results, e.g.:
What is the best way to get a random number? I've googled and found methods X and Y, and wondered which is best for purpose Z.
For example:
var min = -10;
var max = 10;
console.log(Math.floor(Math.random() * (max - min + 1) + min));
.as-console-wrapper { max-height: 100% !important; top: 0; }
See this jsrandom.php for some comparison of Math.floor
, Math.ceil
and Math.round
.