Create a random number between -100 and 100 in JavaScript?

Cool Guy Yo picture Cool Guy Yo · Feb 26, 2012 · Viewed 34.5k times · Source

Possible Duplicate:
Generating random numbers in Javascript

I have the following code var randomnumber=Math.floor(Math.random()*101); that generates a random number for me between 1 and 100. What I would like to do is generate a random number between -100 and 100. I am not sure what to do exactly. Any help would be appreciated.

Answer

Justin Niessner picture Justin Niessner · Feb 26, 2012

First, generate a random number between 1 - 200 then subtract 100:

var randomNumber = Math.floor(Math.random() * 201) - 100;