Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: generating (unique) random numbers for a column

Re: generating (unique) random numbers for a column

From: Mladen Gogala <mgogala_at_adelphia.net>
Date: Mon, 28 Apr 2003 17:58:18 GMT
Message-Id: <pan.2003.04.28.17.58.18.332525@adelphia.net>


On Mon, 28 Apr 2003 17:13:27 +0000, Glen A Stromquist wrote:

> I am trying to populate a column in a table with random numbers with roughly
> the same range as rows in the table, I want these unique so have added the
> constraint. I have a simple trigger that generates a random number before
> insert so when I load the table with sqlldr the numbers are generated.
>
> My problem is that 50 or so of the 500 records get left out because the
> constraint is violated when the dbms_random.value(<range>) tries to pick a
> number it has already generated.
>
> the trigger is as follows:
>
> CREATE OR REPLACE TRIGGER "schema"."GEN_RAND" BEFORE INSERT OR
> UPDATE OF "FIRST_NAME", "LAST_NAME", "ENTERED", "RANDOM" ON
> "COMPANY_LIST"
> FOR EACH ROW
> declare
> x number;
> begin
> x :=dbms_random.value(1,600);
> :new.random := x;
> end;

Well, random means random, not necessarily unique. Have a sequence instead or make an Erathosten's sieve program and pick only the prime numbers. Users would be very happy with a message like:
Enter any 12 digits prime number to continue....

-- 
Mladen Gogala
Software is like sex, it is better when it is free.
Linus Torvalds 
Received on Mon Apr 28 2003 - 12:58:18 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US