Re: Generating random unique alphanumeric IDs

From: Thom Brown <thombrown(at)gmail(dot)com>
To: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Generating random unique alphanumeric IDs
Date: 2009-08-20 12:34:51
Message-ID: bddc86150908200534p14723ac2vd1ef223af095d822@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
>
> Since I'm then converting to_hex to shorten the string I was
> thinking to add some more bits of randomness since eg.
>
> to_hex(10) = 'a'
>
> In the line of
> select lpad(
> to_hex(feistel_encrypt(10)),7 , to_hex((rand()*2^31)::int)
> );
>
> I was wondering if there is any better way to get alphanumeric
> random string quickly. Since uniqueness is assured by passing a
> sequence to fesitel_encrypt, I just need turning into to
> alphanumeric quickly.
>
>
This appears a lot more tricky than I had originally anticipated! I may be
misunderstanding your example, but by alphanumeric, I mean beyond hex (i.e.
a-z and possibly uppcase too).

I've looked into LFSR, but I'm afraid it goes over my head. But what Jason
Betts said seems to summarise what I'm after: "for the OP's problem this
means building a LFSR with n=5c (where c is the number of charactes in the
serial code, and n is the number of bits in the LFSR state) and then taking
a single LFSR result and peeling off 5 bits at a time and using each 5 to
make each charcter in the result."

If this results in an unpredictable and non-duplicating loop of generated
sets of characters, that would be ideal. Would a parallel for this be a
5-character code possibly transcoded from a 6-character GUID/UUID? (a-h +
j+n + p-z + A-H + J-N + P+Z + 2-9 = 56 possible characters, 56^5 =
550,731,776, 550,731,776 / 16 (hex character set) ^ 6 (characters) = just
over 32.), so wouldn't actually use up all possible combinations. :/

Thom

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Karsten Hilbert 2009-08-20 12:52:22 Re: multiple paramters in aggregate function
Previous Message Karsten Hilbert 2009-08-20 12:30:49 Re: Unit conversion database (was: multiple paramters in aggregate function)