On Apr 21, 2009, at 8:02 AM, Edward O'Connor wrote:


I'm currently working on a project where I need a generated unique
identifier for an order and to not use the traditional database
primary key of id[...]
The UUID gem's shortest id is 32 characters and seems a quite lengthy.

Think of it like a URL shortening service. Take several field values,
concatenate them, and hash the result. Then encode the result in
something like base 36, because that's compact and also URL-safe. the
result should only be a few characters long. Store in another column
and everyone's happy.

+1 for the much under-utilized Base-36. Here is how I solved this problem:

rand(2**64).to_s(36) #=>  "3ps8njpa0eqot"

This gives you a 64-bit random identifier in 13 characters. A 64-bit space means that your odds of a collision are less than 0.1% as long as you are generating less than 190 million IDs. (48 bits would get you that same collision rate at 750 thousand IDs.)

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to