2

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?

M Miller
  • 155
  • 4
  • 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 04 '14 at 18:09

1 Answers1

3

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.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
Shaull
  • 17,159
  • 1
  • 38
  • 64
  • 1
    You beat me to it, was writing the same ;) – Denis Feb 04 '14 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 04 '14 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 04 '14 at 18:27
  • 1
    Yes, just note you might get 0, so perhaps always add 1. – Shaull Feb 04 '14 at 18:31