DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: DBMS_RANDOM generates same number .... !!!!

  1. #1
    Join Date
    Sep 2000
    Posts
    47
    Hi,

    Package for random number generation - DBMS_RANDOM generates same number each time I execute it - taking the same seed value. Following is the piece of code:

    dbms_random.seed( 1 ) ;
    rh := dbms_random.value(0, 10) ;
    rm := dbms_random.value(1, 100) ;
    dbms_random.terminate() ;

    Is it required to take a different seed value each time I execute it ????

    And ... what all procedures/functions are included in this package ??

    Thanks in advance ....

  2. #2
    Join Date
    Aug 2000
    Posts
    462
    dbms_random.initialize(12345); -- use at least a 5 digit number

    create or replace procedure getrandom is
    begin
    dbms_output.put_line(dbms_random.random);
    end;
    /


    SQL>execute getrandom
    -1324234211

    Don't reset the seed each time, and the random will work just fine. Procedures/functions include:

    initialize, seed, random, terminate

    See metalink article 1053864.6


  3. #3
    Join Date
    Sep 2000
    Posts
    47
    Hi,

    Try executing the following script :

    /* Begin */
    BEGIN
    DECLARE
    r1 NUMBER ;
    r2 NUMBER ;
    BEGIN
    BEGIN
    dbms_random.seed( 1234567890 ) ;
    r1 := dbms_random.random() ;
    r2 := dbms_random.random() ;
    dbms_random.terminate() ;
    DBMS_OUTPUT.PUT_LINE('R1 : '||r1|| ' R2 : '|| r2 ) ;
    END ;
    BEGIN
    dbms_random.seed( 1234567890 ) ;
    r1 := dbms_random.random() ;
    r2 := dbms_random.random() ;
    dbms_random.terminate() ;
    DBMS_OUTPUT.PUT_LINE('R11 : '||r1|| ' R12 : '|| r2 ) ;
    END ;
    END ;
    END ;
    /* End */

    Try on two different instances of SQL and check out the o/p.

    Well, I used SYSDate and had my work done ...

    Regards,
    Pinakin.

  4. #4
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    Pinakin,

    I can't see what is the point of your previous post.

    Do you want to find out why your PL/SQL code is not working? You have to use DBMS_RANDOM.INITIALIZE to initialize your seed. If you have done so before you've run your anonymous block, then you should use it again after you used DBMS_RANDOM.TERMINATE in your first BEGIN-END block, if you wan't also the second part to succeed. Also, there is no particular need to change your seed very often (if at all) once you initialised it.

    And what do you mean you have your work done by using SYSDATE? You mean you use SYDATE as a direct "semi-random" generator? If so, you could hardly treat that as a random generator.

    Enlighten me if I'm missing the obvious....
    Jurij Modic
    ASCII a stupid question, get a stupid ANSI
    24 hours in a day .... 24 beer in a case .... coincidence?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width