Monday, 26 September 2016

generate random number between 2 variables 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?


Answer



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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...