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

Thread: generate random number

  1. #1
    Join Date
    Feb 2006
    Posts
    86

    generate random number

    I have to generate random number from 1 to 40. I used DBMS_RANDOM.value.

    "
    declare
    num number:=0;
    begin
    num:=round(dbms_random.value(1,10));
    dbms_output.put_line('Number: '||num);
    end;
    "
    when i execute this block, it gives me random number but it is repeating some numbers. For ex 9 is repeating thrice.
    i want that the randome number should exclude the number which it has generated earlier. it should not repeat it.

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    1 to 40 is a pretty small range for random number generation - I would expect lots of "duplicates".

    Having said that, you can always wrap up dbms_random block into a loop where you discard the numbers you don't like and try again.

    oops... that is going to take out some randomblish of dbms_random process, isn't it?
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Feb 2006
    Posts
    86

    random numbers

    bhai 1-to 40 was just an example.anyhow i wanted if any built in function is available with DBMS_RANDOM package to actually givr only unique random numbers....

    is there any ?
    reply awaited.

  4. #4
    Join Date
    Sep 2002
    Location
    England
    Posts
    7,334
    if you want unique why not use a sequence and just get nextval?

  5. #5
    Join Date
    Feb 2006
    Posts
    86

    generate random numbers

    i want unique random numbers..anywaz it seems like no such inbuilt function is avbl with DBMS_RANDOM..i think my logic of putting dbms_random inside a loop & exluding the one which is already generated will do....i just wanted a replacement for my logic with some inbuilt stuff.

  6. #6
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by binoyshil View Post
    i think my logic of putting dbms_random inside a loop & exluding the one which is already generated will do.
    "my logic"? ... you are fast at taking credit for whatever happens around, aren't you? LMAO
    Last edited by PAVB; 12-07-2009 at 08:30 AM.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  7. #7
    Join Date
    Sep 2002
    Location
    England
    Posts
    7,334
    Quote Originally Posted by binoyshil View Post
    i want unique random numbers..anywaz it seems like no such inbuilt function is avbl with DBMS_RANDOM..i think my logic of putting dbms_random inside a loop & exluding the one which is already generated will do....i just wanted a replacement for my logic with some inbuilt stuff.
    unique and random - its impossible to have both becuase you are taking away the randomness of it

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