Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number between negative and positive value [duplicate]

Possible Duplicate:
Generating random numbers in Javascript in a specific range?

How can i get a random value between, for example, from -99 to 99, excluding 0?

like image 838
user1835929 Avatar asked Nov 19 '12 13:11

user1835929


1 Answers

var num = Math.floor(Math.random()*99) + 1; // this will get a number between 1 and 99; num *= Math.round(Math.random()) ? 1 : -1; // this will add minus sign in 50% of cases 

Altogether

var ranNum = Math.ceil(Math.random() * 99) * (Math.round(Math.random()) ? 1 : -1) 
like image 174
LittleSweetSeas Avatar answered Sep 28 '22 21:09

LittleSweetSeas