How to generate account number?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Nov 2 18:39:31 EDT 2012
On Sat, 03 Nov 2012 00:13:19 +0300, Andriy Kornatskyy wrote:
> Requirements for `account number` generator:
>
> 1. Issue pseudo random consistent number (must be unique for dozen
> millions of records)
How much randomness do you need? From the perspective of any one user, a
simple incrementing counter returns arbitrary values, which may be "close
enough" to random.
last_num = 103872 # Pick an arbitrary starting value.
def get_account_number():
"""Return the next account number."""
global last_num
last_num += 1
return last_num
Stick that value in a database instead of a global, and you're done.
What are the consequences of people guessing account numbers? If the
consequences are serious, then you need to make account numbers
cryptographically strong. If the account number alone is not important,
then you don't.
> 2. Easy check validity (without a need to make a database call)
Add a check digit to the number you generate. There are all sorts of ways
to do that. Here are two examples:
http://code.activestate.com/recipes/577692
http://code.activestate.com/recipes/577691
> Interested? Read more here:
If you ask a question here, please keep the discussion here, don't split
it to your personal blog.
Tell us your requirements in more detail, and we will try to help you.
--
Steven
More information about the Python-list
mailing list