Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number with fixed length

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!

like image 640
skywind Avatar asked Dec 12 '11 14:12

skywind


People also ask

How do you generate a random fixed length?

They achieve it by limiting the amount of numbers possible preceding the fixed length. So for instance, a random number of fixed length 2 would be 10 - 99. For 3, 100 - 999. For 4, 1000 - 9999.

Why is 17 the most common random number?

Seventeen is: Described at MIT as 'the least random number', according to the Jargon File. This is supposedly because in a study where respondents were asked to choose a random number from 1 to 20, 17 was the most common choice. This study has been repeated a number of times.

Can random number generator be fixed?

If you know the algorithm and the initial condition, you can crack the sequence of random number generators. True hardware random number generators rely on outside sources for entropy. Those are unlikely ever to be cracked.

How do you generate a random number of fixed length in python?

Using randint() function of python random module, you can generate a random number of specified length, using its minimum and maximum parameters.


1 Answers

   function genRand() {
      return Math.floor(Math.random()*89999+10000);
   }
like image 171
Nate B Avatar answered Oct 03 '22 06:10

Nate B