Creating Unique Account Numbers - Recursive Call

Hi, I need to generate 9 digits of unique account numbers. Here is my pseudo code:

function generateAccNo()

    generate an account number between 100,000,000 and 999,999,999

    if the account number already exists in the DB 
        call generateAccNo()    /* recursive call */
    else
        return new accout number
    end if

end function

The function works well, however I'm a little worried about the recursive call.

Will this cause a memory leak (PHP 5 under apache)?

Is this an acceptable way to solve this problem?

Thanks for your input.

+5
source share
10 answers

You understand that this can lead to a stack overflow, right? As the number of customesr increases, the likelihood of not finding an acceptable account number increases.

, ? id .

, , - . ( ), .

:
, . , db ( id), . , .

+8

, , , . :

. GUID

, GUID, , , , . , . AccountRecordId, ,

. : +

, , db 5 ( ), 5 . , ,

+3

. while ,

function generateAccNo()

    generate an account number between 100,000,000 and 999,999,999

    while ( the account number already exists in the DB ) {
         generate new account number;
    }
    return new account number

end function

- , , - , .

+2

, , , - , , ?

, , - , , , ad-nauseum.

+1

- - - .

+1

, , . . , . , , , → . , , , .

, , , . + TheZenker. .

+1

. .

0

while:

function generateAccNo()

    while (true) {    

      generate an account number between 100,000,000 and 999,999,999

      if the account number already exists in the DB 
          /* do nothing */
      else
          return new accout number
      end if
    }

end function
0

:

lock_db
do
    account_num <= generate number
while account_num in db

put row with account_num in db

unlock_db
0

Why not process this database? In SQL Server, you can just have an identifier column that starts with 100000000. Or you can use sql in any db that you have. Just get max id plus 1.

0
source

All Articles