Skip site navigation (1) Skip section navigation (2)

Re: Re: Re: Re: RANDOM function?

From: ghaverla(at)freenet(dot)edmonton(dot)ab(dot)ca
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Re: Re: Re: RANDOM function?
Date: 2001-07-31 13:58:21
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
On Tue, 31 Jul 2001, Einar Karttunen wrote:
> On Tue, 31 Jul 2001, Neal Lindsay wrote:
> > I think that what you want to do is create a trigger (or rule?) that pulls
> > up your random number and then checks to see if it exists already.  I
> > haven't used triggers yet, but it should not be too hard.  Also, what
> > happens when you declare a column unique, and a function like random()
> > tries to give it a non-unique number?
> >
> This can be accomplished very easily.
> 	col integer default (random()*2147483647) unique,
> 	col2 text
> 	);
> INSERT INTO tbl (col2) VALUES ('the value of col2');
> the insert will fail if the number is not unique then just perform it
> anew.

The only thing I would add, is your multiplier (2147...) must
always be big, with respect to how many times you want to draw
this random number without replacement.  If you were wanting
to draw 2000000000 random numbers, this method would bog 
down in rejections (insert failures) towards the end.


Matter Realisations
Gordon Haverland, B.Sc. M.Eng. President
101  9504 182 St. NW    Edmonton, AB, CA  T5T 3A7
780/481-8019            ghaverla @
780/993-1274 (cell)

In response to


pgsql-novice by date

Next:From: Einar KarttunenDate: 2001-07-31 14:09:07
Subject: Re: Re: Re: Re: RANDOM function?
Previous:From: Einar KarttunenDate: 2001-07-31 13:30:23
Subject: Re: Re: RANDOM function?

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group