Question

I need to generate unique non-sequential alphanumeric string of exactly length 6 (not less, not more). With non-sequential I mean it should look visually random at first sight and not coming from a sequence (it doesn't matter if you can find the sequence after some number of days of study, it should just appear random at first sight). And bear in mind it must be unique. It's also unfeasible to lookup all the numbers that have been used already.

I was thinking of letting a database generate a unique number and then apply some function to convert that number into a unique non-sequential alphanumeric string of exactly length 6. Similar to how a hashing algorithm works, but then without possible collisions.

I've found this: http://blog.maxant.co.uk/pebble/2010/02/02/1265138340000.html But the sequence is not visually random.

Was it helpful?

Solution

Is it possible for you to register the generated String sequence to some database/file/someplace? Then one way to do can be for each position (from 1 to 6), randomly pick a character and form your 6 character string. And see if it is one of the registered sequence or not. If already registered, then generated another sequence. If not registered then register & use the generated one for your purpose.

OTHER TIPS

By alphanumeric are you aiming at [A-Z0-9], [a-zA-Z0-9] or would [a-zA-Z0-9\+\\] be acceptable? In the latter case you could cheat, XOR your unique ID with suitably large value and then throw the whole lot through a Base64 algorithm, saving work.

For the other two, take a leaf out of the simple pseudo-random number generator's book; Perform something like x = ((id + salt)*multiplier) mod pow(alphabet_size,6) and then 'decode' the integer x into alphanumeric characters. Of course choosing a good salt and multiplier is important; The latter would ideally be a large prime or at least be coprime to the alphabet_size. The salt could even be zero, but can be used to start at a more aesthetic value for the output if you wish.

As long as the initial id never exceeds pow(alphabet_size,6), the hash will be uniquely reversible. You would need to convert your hash back into an integer and then use a modular division algorithm to re-obtain (id + salt), and thus the original id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top