I want to generate a random integer number with 0-9 numbers and with length = 5. I try this:
function genRand(min,max) {
for (var i = 1; i <= 5; i++) {
var range = max - min + 1;
return Math.floor(Math.random()*range) + min;
}
}
and call:
genRand(0,9);
But it always returns 1 number, not 5 (
Help please!
The smallest 5 digit number is 10000, the largest is 99999, or 10000+89999.
Return a random number between 0 and 89999, and add it to the minimum.
Math.floor rounds down, and Math.random is greater than or equal to 0 and less than 1.
Here's a more generalized version of Nate B's answer:
To get a 5 digit random number generate random numbers between the range (10000, 99999). Or generate randomly 5 single digits and paste them.
EDIT
The process you have shown simply will generate one number and return to the caller. The think which might work is (pseudo code) :
Or better generate 5 digit random numbers with
rand (10000, 99999)
return
exits the function on the first loop.