Re: Generating random unique alphanumeric IDs

From: Thom Brown <thombrown(at)gmail(dot)com>
To: Sam Mason <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Generating random unique alphanumeric IDs
Date: 2009-08-16 11:57:34
Message-ID: bddc86150908160457u5332a57cl7e2410dd4f1fa9a8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
> The following is the obvious PGSQL code, you'd obviously need something
> else to stop duplicates.
>
> SELECT array_to_string(array((
> SELECT SUBSTRING('abcdefghjklmnpqrstuvwxyz23456789'
> FROM mod((random()*32)::int, 32)+1 FOR 1)
> FROM generate_series(1,5))),'');
>
> As this only generates five characters and each character can be one of
> 32 values, you've got about 33554432 choices and you'd have a 50% chance
> of getting a duplicate after 7240 values. This assumes I wrote the
> above code correctly. It's also not amazing because PG's random number
> generator is defined to return a value between 0 and 1 inclusive, it's
> generally much more useful if it runs from 0 to less than 1 and would
> mean that I wouldn't need the "mod" above and would remove the (slight)
> biasing towards choosing 'a'.
>

That does actually work! I'm not sure why you're saying that there's a 50%
chance of duplication after 7240 values though. With 33 million
combinations, I would have thought that duplications would become equally
likely at the 16,777,216 mark.

I hadn't thought of coding it the way you did, which is an interesting way
of approaching it!

Thom

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bastiaan Wakkie 2009-08-16 12:01:25 Re: A history procedure that prevents duplicate entries
Previous Message Sam Mason 2009-08-16 11:48:39 Re: Generating random unique alphanumeric IDs