PHP – Generating Large Random Numbers vs. Each Digit

PHPrandom

I'm using a PRNG (PHP's default rand() implementation) to generate unique 16 digit numbers. Does it make sense to generate digits one by one, or the number at once?

I'm using PHP.

Best Answer

In principle each arithmetic pseudo-random number generator (such as PHP's) has a finite period after which the same sequence of numbers will repeat. And most generators will generate a fixed minimum number of bits (often 32) for you internally at each step, no matter how large or small the range of results is that you asked for.

So generating each digit separately will eat up the sequence of your generator more quickly and thus make the resulting sequence of complete numbers less 'random' overall.

In practice the most relevant difference is probably that generating each digit separately is a lot more work.

Both of these effects, however, point in the same direction: You should generate your whole random number at once.

Related Topic