Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random numbers in Javascript [duplicate]

Tags:

javascript

Possible Duplicate:
Generating random numbers in Javascript

Hi..

I want to generate random numbers (integers) in javascript within a specified range. ie. 101-999. How can I do that. Does Math.random() function supports range parameters ?

like image 963
Vineet Sharma Avatar asked Nov 16 '10 13:11

Vineet Sharma


1 Answers

Just scale the result:

function randomInRange(from, to) {
  var r = Math.random();
  return Math.floor(r * (to - from) + from);
}
like image 70
Pointy Avatar answered Oct 03 '22 15:10

Pointy