views:

100

answers:

2

Hello, I'm trying to generate UUIDs with the same style as bit.ly urls like:

http://bit.ly/aUekJP

or cloudapp ones:

http://cl.ly/1hVU

which are even smaller

how can I do it? I'm now using UUID gem for ruby but I'm not sure if it's possible to limitate the length and get something like this. I am currently using this:

UUID.generate.split("-")[0] => b9386070

But I would like to have even smaller and knowing that it will be unique.

Any help would be pretty much appreciated :)

A: 

The only way to guarantee uniqueness is to keep a global count and increment it for each use: 0000, 0001, etc.

Aardsquid
+2  A: 

You are confusing two different things here. A UUID is a universally unique identifier. It has a very high probability of being unique even if millions of them were being created all over the world at the same time. It is generally displayed as a 36 digit string. You can not chop off the first 8 characters and expect it to be unique.

Bitly, tinyurl et-al store links and generate a short code to represent that link. They do not reconstruct the URL from the code they look it up in a data-store and return the corresponding URL. These are not UUIDS.

Without knowing your application it is hard to advise on what method you should use, however you could store whatever you are pointing at in a data-store with a numeric key and then rebase the key to base32 using the 10 digits and 22 lowercase letters, perhaps avoiding the obvious typo problems like 'o' 'i' 'l' etc

EDIT

On further investigation there is a Ruby base32 gem available that implements Douglas Crockford's Base 32 implementation

A 5 character Base32 string can represent over 33 million integers and a 6 digit string over a billion.

Steve Weet
thanks for the information Steve, yes, I think I really misunderstood the uuid principle :/ the base32 gem seems very nice, I'm just trying to figure out a proper way to generate short urls based on record ids, I was using a string ID before and assigning the UUID method to it, but I think now I can come back to an integer ID and then if for example, start the id counting at 363012 for example, I would get a 'B2G4' string representation for it. I hope this could follow the same as you've explained above.
ludicco
@ludicco. That is exactly what I was saying.
Steve Weet