Re: Generating random unique alphanumeric IDs

From: Bob Gobeille <bob(dot)gobeille(at)hp(dot)com>
To: Thom Brown <thombrown(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Generating random unique alphanumeric IDs
Date: 2009-08-16 22:53:01
Message-ID: A63B6C02-F405-4167-97E6-ABAD1F53D039@hp.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Aug 16, 2009, at 5:07 AM, Thom Brown wrote:

> Does anyone know a way to generate a random and unique lowercase
> alphanumeric ID (preferably without using 0, 1, o or i to prevent
> problems with users manually typing the ID) using SQL without
> resorting to a prerendered table or using GUIDs.
>
> For example, if I were to ask for an ID of 5 characters, something
> like the following would be returned:
>
> hn21o
> 8sp2j
> 9wwun
> m7z02
>
> Notice that I don't mean hexadecimal values either. This would
> preferrably not resort to trying to generate the ID, then checking
> for a clash, and if there is one, do it again, although that could
> do as I can't think of how the ideal solution of a ID hashing
> algorithm would be possible.

One way is to use a LFSR (linear feedback shift register function). I
haven't used one in a long time but I recall generating pseudo random
numbers that are guaranteed not to repeat after billions of
iterations. It's very fast as well. Then translate the resulting
integer into the character sequence of your choosing. Here is a
reference: http://en.wikipedia.org/wiki/Linear_feedback_shift_register

Bob Gobeille
Hewlett Packard
Open Source Program Office
(and http://fossology.org)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2009-08-17 00:10:17 Re: 8.4 rpm packaging problem?
Previous Message Devrim GÜNDÜZ 2009-08-16 22:45:00 Re: 8.4 rpm packaging problem?