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?
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.
var min = -10;
var max = 10;
// and the formula is:
var random = Math.floor(Math.random() * (max - min + 1)) + min;
For example:
var min = -10;
var max = 10;
Math.floor(Math.random() * (max - min + 1) + min)
see http://thepenry.net/jsrandom.php for some comparaison of Math.floor
, Math.ceil
and Math.round