So I have been trying to build a URL shortner but I am unable to generate unique but random strings. I have look everywhere for a solution but couldn't find one thus posting it here.
I have a table in which I get auto-generated sequential primary key (ID) against a inserted record. Now I take that ID and run a bijective function on it that turns
0 → a
1 → b
...
25 → z
...
52 → 0
61 → 9
Now the issue is that the generated string is not random. For example :
63 --> b1
64 --> b2
...
1836 --> bpa
1836 --> bpb
Which is very guessable. I have even tried to encode the ID to Base64 but the resultant string is again guessable and if I use GUID instead and encode it to Base64 then the resultant string is very large. The max string should be of 7,8 characters - ideally 3,4 chars.
I am wondering how does bit.ly does it? their generated short URL is always unique and random.
What you do is generate sequential keys. The first URL is 1, next is 2, etc. But you obfuscate those keys by uniquely mapping each one to a different number. So 1 might become 76839427, and 2 might become 9935. Then you base64 encode the number.
The nice thing is that all you have to keep track of is the next sequential number. The process is reversible, so you can turn 9935 back into 2.
I give an example of the mapping in Efficient algorithm for generating unique (non-repeating) random numbers.
Another possibility is to use a Linear Feedback Shift Register with a long period. You can create one with a period of 2^64. Guaranteed not to repeat until you've generated 2^64 numbers.
Note that neither of these is truly "random." They're methods of obfuscation, but given enough effort, somebody could crack the algorithm. But then, they could crack a pseudo-random number generator, too.