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: DBMS_RANDOM package with negative random numbers ??

Re: DBMS_RANDOM package with negative random numbers ??

From: Thomas Kyte <tkyte_at_oracle.com>
Date: 31 May 2002 13:19:47 -0700
Message-ID: <ad8lt301mrj@drn.newsguy.com>


In article <32fe19ad.0205310833.4343e8d0_at_posting.google.com>, rolf.unger_at_ctilabs.de says...
>
>Hi all,
>
>has anybody experience with the package DBMS_RANDOM
>on Windows NT?
>Is my perspective wrong if I say:
> "Random numbers have to be positiv!"
>
>I have worked with C, Perl, JavaScript, (Visual) Basic
>and in all those languages (or in their libary functions)
>random numbers are positiv!
>So why not in PL/SQL?
>Is there a type cast, that I missed?
>Is the problem related to the int representation on WinNT?
>Or is it due to my rather old Oracle Version 8.0.4?
>

Well -- I never saw an ANSI spec on "random functions" that says "they must be positive". In any case, the solution is relatively simple.

     v_nInt := ABS(dbms_random.random());

>I included the following code snippet, that makes use of the
>dbms_random package.
>
>DECLARE
> v_nInt BINARY_INTEGER;
> v_nRandom number(20);
>BEGIN
> Select hsecs Into v_nRandom From v$timer;
> dbms_output.put_line( v_nRandom );
> dbms_random.initialize(v_nRandom);
> FOR i IN 1..10
> LOOP
> v_nInt := dbms_random.random();
> v_nRandom := v_nInt;
>-- dbms_output.put_line( v_nInt );
> dbms_output.put_line( v_nRandom );
> END LOOP;
> dbms_random.terminate();
>END;
>/
>
>288330377
>-1420311009
>-1168730824
>1601612920
>-408652530
>635338897
>1636768007
>-618724749
>-1679183912
>-816529764
>1734396077
>
>PL/SQL procedure successfully completed.
>
>
>As you see I also tried to output the BINARY_INTEGER directly
>with exactly the same results as with the NUMBER variable.
>Not sure if dbms_output.put_line() is the right way to look at
>a variable's value, as it probably does some type conversion
>internally.
>
>Any comment is appreciated.
>Thanks, Rolf.

--
Thomas Kyte (tkyte@oracle.com)             http://asktom.oracle.com/ 
Expert one on one Oracle, programming techniques and solutions for Oracle.
http://www.amazon.com/exec/obidos/ASIN/1861004826/  
Opinions are mine and do not necessarily reflect those of Oracle Corp 
Received on Fri May 31 2002 - 15:19:47 CDT

Original text of this message

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