1

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.

  • It depends upon the actual implementation and system. – Basile Starynkevitch Dec 1 '14 at 12:13
  • 1
    Generating digit by digit is a significant extra effort (primarily for you) and another source of potential bugs. So unless generating digit by digit actually results in higher quality numbers (which you don't seem to believe either, from the phrasing of the title), no it does not make sense. – user7043 Dec 1 '14 at 12:16
  • @delnan, this is my question exactly. I don't know what's better. I had a feeling that generating one by one would be bad since i would be basically generating sequences of numbers and PRNGs use previous number to generate a new number. But i can't really prove it. – buskila Dec 1 '14 at 12:41
  • @Oleg If number generation is dependant on the previous number, then generating one by one is a very bad idea. There would only be 2^32 * 10 unique values (if the seed is 32 bits), instead of the ~2^53 possible 16-digit numbers. – Rotem Dec 1 '14 at 13:04
  • In general it will be a lot slower and you will still have all the problems of the underlying PRNG. What exactly do you mean by "more unique" anyway? Is it predictability? Closer to a HRNG? Collisions if used as an UID generator? – Ordous Dec 1 '14 at 13:24
1

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.

0

With a PRNG that has equal probability for "single decimal digits" as well as "16-digit decimal numbers", no, they should be equal.

However, the PRNG is unlikely to have perfect probability for base-10 numbers, so "possibly". It would depend on how much this influences the probabiity for any specific digit.

Bear in mind that a 16-digit number is up to 54 bits, so this may influence your decision as to if you should generate you number in pieces or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.