We have a form with 2 fields and a button. We want on button click to output random int number (like 3, 5 or 33) which would lie between int A and int B? (no use of jQuery or anything like it is required)
We have a form with 2 fields and a button. We want on button click to output random int number (like 3, 5 or 33) which would lie between int A and int B? (no use of jQuery or anything like it is required)
Share Improve this question edited Dec 30, 2010 at 3:42 user372551 asked Dec 29, 2010 at 23:37 RellaRella 66.9k112 gold badges373 silver badges642 bronze badges3 Answers
Reset to default 13You can use Javascript Math.random
function randomInRange(start,end){
return Math.floor(Math.random() * (end - start + 1) + start);
}
Use something like Math.floor(Math.random()*(intB-intA +1)) + intA
?
Like this:
Math.floor(a + Math.random() * (b - a))
The Math.random()
method returns a random floating-point number in the range [0,1) — that is, between 0 (inclusive) and 1 (exclusive).