Take the 2-minute tour ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

I'm not sure how to word this because I'm not familiar with this, but I'm sure a process like this is rather common.

Basically, I've got members signing up for our website, and each one is assigned a normal sequential ID (a MySQL auto_increment ID), i.e. 1, 2, 3, ....

The client wants these users to have a "member number" -- something around 8 digits long. It doesn't matter if the algorithm is predictable, I just want to generate an 8-digit number that I know won't repeat and isn't totally obvious that it's sequential, e.g. I don't want to use 10000001, 10000002, 10000003....

Is there a simple algorithm for generating "member numbers" in a case like this? Like I said, I don't care if it's reverse-engineerable, just as long as I know it will always be unique.

For instance, I considered just taking a hash and truncating it, e.g. md5(1), md5(2), md5(3)` in base 10 and taking the first 8 characters -- but there's always a chance of conflict, where two IDs produce a similar hash.

Any thoughts?

share|improve this question
    
I don't understand why this is even an issue. If your client is worried people see that they are only the 15th customer, count down from 999999999. –  Raphael Feb 4 at 18:09
add comment

1 Answer

up vote 2 down vote accepted

Not sure what you mean by "totally obvious". Technically, every permutation of $\{1,...,10^8\}$ would work here. You can randomly generate such a permutation, store it in an array, and use this array as an index.

If you want a specific example, which may look at first glance non-sequential, take some big number $p$ such that $gcd(p,10^8)=1$ (e.g. $p= 19683$), then the id of user $i$ will be $i*p (\text{ mod } 10^8)$. This is guaranteed to be unique for each user (up to $10^8$ users).

It may look random only at a glance, but anyone who takes enough time can figure out this is not random.

share|improve this answer
1  
You beat me to it, was writing the same ;) –  Denis Feb 4 at 17:02
    
I mean, I don't care if it's actually predictable to anyone who can do math, just so that, at a glance, it isn't just a particular positioned index that's incrementing. Works for me, thanks! –  M Miller Feb 4 at 17:47
    
Programming this, is this the math I would use? number = id * P % pow(10, 8), where P is a number like 19683. –  M Miller Feb 4 at 18:27
    
Yes, just note you might get 0, so perhaps always add 1. –  Shaull Feb 4 at 18:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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