Question

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.

Was it helpful?

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.

OTHER TIPS

What is you requirement? I guess you need to generate unique random number.

Step 2 in your case may take long time depending on the number of entries.

to get a good pseudo random number using any of the hashing algorithms like SHA1 or MD5

Here also there is a rare possibility that two entries can have same random number

To make it completely unique, you can club the random number with a unique id of the entry.

for eg. you have 100 entries with unique ids 1 to 100. to get a unique random number for 100th entry, Generate a random number say 1129642347 and club it with the unique id 100. As a simple method you can use concatenating. Then the random number becomes 1129642347100

LukeH have a point here. Credit card numbers are not random, they just numbers that are valid against the Luhn algorithm. The number back in the card (the security code number with 3 or 4 digits) should be a random number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top