Re: Generating random unique alphanumeric IDs

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>, pgsql-general(at)postgresql(dot)org, thombrown(at)gmail(dot)com
Subject: Re: Generating random unique alphanumeric IDs
Date: 2009-08-17 05:27:21
Message-ID: dcc563d10908162227n21fae66em448e467b63893192@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Aug 16, 2009 at 6:12 PM, Alvaro
Herrera<alvherre(at)commandprompt(dot)com> wrote:
> It's in the wiki, in the Snippets area.
> wiki.postgresql.org/wiki/Snippets
> (pseudo encrypt or something like that I think it's called)

Here's a simple 255 value linear feedback shift register. It's
nothing fancy, but works as an example. It's not any kind of a secure
sequence, but can be handy for generating pseudo random codes for
things like identifiers that need to not be sequential.

create table lfsr (b bit(8));
insert into lfsr values ('10100011');
create or replace function lf() returns bit(8) language sql as $$
update lfsr set b=(select
((substring(b,1,1)#substring(b,3,1)#substring(b,4,1)#substring(b,5,1)))::bit(8)>>7|(b<<1)
from lfsr) ;
select b from lfsr $$;
create table l (b bit(8), i int);
insert into l select lf(),generate_series(1,255);
select count(distinct(b)) from l;
select b, count(b) from l group by b having count(b) > 1;
insert into l select lf(),generate_series(1,1);
select b, count(b) from l group by b having count(b) > 1;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Fetter 2009-08-17 05:45:43 Re: plpgsql function to validate e-mail
Previous Message Andrew Bartley 2009-08-17 05:24:49 Re: Function Logging