I would like to generate a code like goo.gl and jsfiddle websites (http://jsfiddle.net/XzKvP/
).
I tried different things that give me too large of a guid, a repeating alphanumeric code, etc.
I'm thinking I should be able to generate an alphanumeric code based on the Primary Key in my database table. This way it will be non-repeating? The PK is an auto-incremented integer by 1. But not sure that's how it should be done.
I want the code to look random, but it does NOT have to be. For example, I do NOT want item 1234
in my database to be BCDE
and the 1235
item to be BCDF
.
Examples:
Notice how the url http://jsfiddle.net/XzKvP/
has a unique 5 character code XzKvP
associated to the page. I want to be able to generate the same type of code.
goo.gl does it too: http://goo.gl/UEhtg has UEhtg
How is this done?
The solutions based on a random substring are no good because the outputs will collide. It may happen prematurely (with bad luck), and it will eventually happen when the list of generated values grows large. It doesn't even have to be that large for the probability of collisions to become high (see birthday attack).
What's good for this problem is a pseudo random permutation between the incrementing ID and its counterpart that will be shown in the URL. This technique guarantees that a collision is impossible, while still generating into an output space that is as small as the input space.
Implementation
I suggest this C# version of a Feistel cipher with 32 bits blocks, 3 rounds and a round function that is inspired by pseudo-random generators.
private static double RoundFunction(uint input) { // Must be a function in the mathematical sense (x=y implies f(x)=f(y)) // but it doesn't have to be reversible. // Must return a value between 0 and 1 return ((1369 * input + 150889) % 714025) / 714025.0; } private static uint PermuteId(uint id) { uint l1=(id>>16)&65535; uint r1=id&65535; uint l2, r2; for (int i = 0; i < 3; i++) { l2 = r1; r2 = l1 ^ (uint)(RoundFunction(r1) * 65535); l1 = l2; r1 = r2; } return ((r1 << 16) + l1); }
To express the permuted ID in a base62 string:
private static string GenerateCode(uint id) { return ToBase62(PermuteId(id)); }
The Base62
function is the same as the previous answer except that is takes uint
instead of int
(otherwise these functions would have to be rewritten to deal with negative values).
Customizing the algorithm
RoundFunction
is the secret sauce of the algorithm. You may change it to a non-public version, possibly including a secret key. The Feistel network has two very nice properties:
even if the supplied RoundFunction
is not reversible, the algorithm guarantees that PermuteId()
will be a permutation in the mathematical sense (wich implies zero collision).
changing the expression inside the round function even lightly will change drastically the list of final output values.
Beware that putting something too trivial in the round expression would ruin the pseudo-random effect, although it would still work in terms of uniqueness of each PermuteId
output. Also, an expression that wouldn't be a function in the mathematical sense would be incompatible with the algorithm, so for instance anything involving random()
is not allowed.
Reversability
In its current form, the PermuteId
function is its own inverse, which means that:
PermuteId(PermuteId(id))==id
So given a short string produced by the program, if you convert it back to uint
with a FromBase62
function, and give that as input to PermuteId()
, that will return the corresponding initial ID. That's pretty cool if you don't have a database to store the [internal-ID / shortstring] relationships: they don't actually need to be stored!
Producing even shorter strings
The range of the above function is 32 bits, that is about 4 billion values from 0 to 2^32-1
. To express that range in base62, 6 characters are needed.
With only 5 characters, we could hope to represent at most 62^5
values, which is a bit under 1 billion. Should the output string be limited to 5 characters, the code should be tweaked as follows:
find N
such that N
is even and 2^N
is as high as possible but lower than 62^5
. That's 28, so our real output range that fits in 62^5
is going to be 2^28
or about 268 million values.
in PermuteId
, use 28/2=14
bits values for l1
and r1
instead of 16 bits, while being careful to not ignore a single bit of the input (which must be less than 2^28).
multiply the result of RoundFunction
by 16383 instead of 65535, to stay within the 14 bits range.
at the end of PermuteId
, recombine r1
and l1
to form a 14+14=28
bits value instead of 32.
The same method could be applied for 4 characters, with an output range of 2^22
, or about 4 million values.
What does it look like
In the version above, the first 10 produced strings starting with id=1 are:
cZ6ahF 3t5mM xGNPN dxwUdS ej9SyV cmbVG3 cOlRkc bfCPOX JDr8Q eg7iuA
If I make a trivial change in the round function, that becomes:
ey0LlY ddy0ak dDw3wm bVuNbg bKGX22 c0s5GZ dfNMSp ZySqE cxKH4b dNqMDA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With