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?

  • $\begingroup$ 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. $\endgroup$
    – Raphael
    Feb 4, 2014 at 18:09

1 Answer 1


Not sure what you mean by "totally obvious". Technically, every permutation of $\{1,\dots,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 \cdot p \pmod{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.

  • 1
    $\begingroup$ You beat me to it, was writing the same ;) $\endgroup$
    – Denis
    Feb 4, 2014 at 17:02
  • $\begingroup$ 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! $\endgroup$
    – M Miller
    Feb 4, 2014 at 17:47
  • $\begingroup$ Programming this, is this the math I would use? number = id * P % pow(10, 8), where P is a number like 19683. $\endgroup$
    – M Miller
    Feb 4, 2014 at 18:27
  • 1
    $\begingroup$ Yes, just note you might get 0, so perhaps always add 1. $\endgroup$
    – Shaull
    Feb 4, 2014 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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