Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate random number between 2 variables jquery [duplicate]

Tags:

jquery

i need to generate a random number between -100 and a variable 'max_top' ( will always be a positive number, up to roughly 40). The number needs to be able to be a minus as well as positive.

how can i do this?

like image 924
rpsep2 Avatar asked Mar 12 '14 20:03

rpsep2


1 Answers

You dont need JQuery for this:

var minNumber = -100;
var maxNumber = 40

var randomNumber = randomNumberFromRange(minNumber, maxNumber);

function randomNumberFromRange(min,max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
}

console.log(randomNumber);

jsFiddle DEMO

like image 141
Turnip Avatar answered Sep 28 '22 08:09

Turnip