Search code examples
algorithmrandomnumbersprobabilityprng

Random Number Generator Algorithm


What algorithm is used by banks for generating random numbers like(credit cards/debit cards numbers) ?

Suppose i maintain all the numbers in DB and If i try the below approach,

  1. Generate a random number.
  2. Verify whether the number has been already assigned.
  3. If yes goto step 1.
  4. If no create a record in DB for the new number and output the result.

It will ask for more db hits when the card volume increases.

Any other take on this ?? Please help.


Solution

  • There are three general solutions to the non-duplicate random number problem:

    1. If you want a few numbers from a large range then pick one and reject it if it is a duplicate. If the range is large, then this won't cause too many repeated attempts. This is what you mention above.

    2. If you want a lot of numbers from a small range, then set out all the numbers in an array and shuffle the array. The Fisher-Yates algorithm is standard for array shuffling. Take the random numbers in sequence from the shuffled array.

    3. If you want a lot of numbers from a large range then use an appropriately sized encryption algorithm. E.g. for 64 bit numbers use DES and encrypt 0, 1, 2, 3, ... in sequence. The output is guaranteed unique because encryption is reversible. The Hasty Pudding Cipher can be set for any convenient range of numbers.