DBMS_RANDOM
Could anyone please let me know how to use the dbms_random package?
I used DBMS_RANDOM in my procedure but I noticed that the random repeated a number. I think this is strange. I ran the code again and it worked well. Is it possible that the DBMS_RANDOM procedure may repeat a number. Below is the output screen and my code.
SQL> BEGIN
2 RANDOM_SAMPLING;
3 COMMIT;
4 END;
5 /
The Range numbers: 1350467 : 1350485
This is the range to insert: 1350480
This is the range to insert: 1350484
This is the range to insert: 1350484
PL/SQL procedure successfully completed.
M_num := 1350485 - 1350467+ 1;
WHILE COUNTER < MARK_20_PERCENT
LOOP
RANDNUM := mod(abs(dbms_random.random), M_NUM )+1350467;
DBMS_OUTPUT.PUT_LINE(' About to insert. This is the range to insert: '||RANDNUM);
INSERT INTO T
SELECT *
FROM b
WHERE T_KEY = RANDNUM;
COUNTER := COUNTER + 1;
END LOOP;
Edited by: user8738152 on May 27, 2011 12:10 PM
Similar Messages
-
Error while inserting data using DBMS_RANDOM
Hi ,
I tried the following insert command in the link http://www.oracle-base.com/articles/misc/RollupCubeGroupingFunctionsAndGroupingSets.php
BANNER
Oracle Database 10g Release 10.2.0.1.0 - Production
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> INSERT INTO dimension_tab
2 SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
3 TRUNC(DBMS_RANDOM.value(low => 1, high => 6)) AS fact_2_id,
4 TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_3_id,
5 TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_4_id,
6 ROUND(DBMS_RANDOM.value(low => 1, high => 100), 2) AS sales_value
7 FROM dual
8 CONNECT BY level <= 1000;
SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
ERROR at line 2:
ORA-00907: missing right parenthesisCould you please advice me to get the correct recordssmile wrote:
Hi ,
I tried the following insert command in the link http://www.oracle-base.com/articles/misc/RollupCubeGroupingFunctionsAndGroupingSets.php
BANNER
Oracle Database 10g Release 10.2.0.1.0 - Production
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> INSERT INTO dimension_tab
2 SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
3 TRUNC(DBMS_RANDOM.value(low => 1, high => 6)) AS fact_2_id,
4 TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_3_id,
5 TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_4_id,
6 ROUND(DBMS_RANDOM.value(low => 1, high => 100), 2) AS sales_value
7 FROM dual
8 CONNECT BY level <= 1000;
SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
ERROR at line 2:
ORA-00907: missing right parenthesisCould you please advice me to get the correct recordsYou can't used named parameters for function/procedure calls in SQL until version 11.
You'll have to remove the
low => And stick with the good old "positional" way of doing things. -
Error from dbms_random package
I have tried unsuccessfully to get the dbms_random package installed. This is my latest error message. Can anyone help me out?
call dbms_random.initialize(12345)
ERROR at line 1:
ORA-04068: existing state of packages has been discarded
ORA-04063: package body "SYS.DBMS_CRYPTO_TOOLKIT" has errors
ORA-06508: PL/SQL: could not find program unit being called
ORA-06512: at "SYS.DBMS_RANDOM", line 19
ORA-06512: at line 1
nullIf you are running Oracle8 Release 8.0.3, DBMS_RANDOM may not have been installed in your database. In this case, you need to execute the following scripts in the specified order from within your SYS account: dbmsoctk.sql, prvtoctk.plb, and finally dbmsrand.sql (it contains both the package specification and body for DBMS_RANDOM).
Good Luck -
hallo,
i would like to know how DBMS_RANDOM package can be implemented.
can you please explain with example.
is there any other means of performimg random sampling?
thank you
regards,
raoHi
Desc DBMS_RANDOM;
PROCEDURE INITIALIZE
Názov argumentu Typ In/Out predvolene?
VAL BINARY_INTEGER IN
FUNCTION NORMAL RETURNS NUMBER
FUNCTION RANDOM RETURNS BINARY_INTEGER
PROCEDURE SEED
Názov argumentu Typ In/Out predvolene?
VAL BINARY_INTEGER IN
PROCEDURE SEED
Názov argumentu Typ In/Out predvolene?
VAL VARCHAR2 IN
FUNCTION STRING RETURNS VARCHAR2
Názov argumentu Typ In/Out predvolene?
OPT CHAR IN
LEN NUMBER IN
PROCEDURE TERMINATE
FUNCTION VALUE RETURNS NUMBER
FUNCTION VALUE RETURNS NUMBER
Názov argumentu Typ In/Out predvolene?
LOW NUMBER IN
HIGH NUMBER IN
Example in SQL+:
Exec DBMS_RANDOM.INITIALIZE(100);
SELECT DBMS_RANDOM.Normal
, DBMS_RANDOM.RANDOM
, DBMS_RANDOM.VALUE()
, DBMS_RANDOM.VALUE(10,100)
FROM DUAL;
milos -
DBMS_RANDOM and ORA-01436: CONNECT BY loop in user data
Hi,
The following query gets the error "ORA_01436: CONNECT BY loop in user data":
SELECT currentdate
, ADD_MONTHS ( currentdate
, (LEVEL - 1) * months
) + ((LEVEL - 1) * days) AS final_date
, level
FROM table_z
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY currentdate = PRIOR currentdate
AND PRIOR dbms_random.value IS NOT NULL
AND ADD_MONTHS ( currentdate
, (LEVEL - 2) * months
) + ((LEVEL - 2) * days) <= TO_DATE ( '05/01/2008'
, 'MM/DD/YYYY'
);I've seen "PRIOR dbms_random.value IS NOT NULL" used like this as as a way to get around ORA-01436 in Oracle 9, but I'm using Oracle 11, and it doesn't work
currentdate is unique.
If I hard-code any one set of values, like this:
DEFINE currentdate = "TO_DATE ('03/02/2006', 'MM/DD/YYYY')"
DEFINE days = 300
DEFINE months = 0
SELECT ¤tdate
, ADD_MONTHS ( ¤tdate
, (LEVEL - 1) * &months
) + ((LEVEL - 1) * &days) AS final_date
FROM dual
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY ¤tdate = PRIOR ¤tdate
AND PRIOR dbms_random.value IS NOT NULL
AND ADD_MONTHS ( ¤tdate
, (LEVEL - 2) * &months
) + ((LEVEL - 2) * &days) <= TO_DATE ( '05/01/2008'
, 'MM/DD/YYYY'
);I can get the correct results from dual. I was looking for a way to extend this to get multiple rows from table_z in the same query.
The problem came up in [this thread|http://forums.oracle.com/forums/thread.jspa?threadID=894946&tstart=0], where the problem is:
Given this table:
CREATE TABLE table_z
( currentdate DATE
, months NUMBER
, days NUMBER
INSERT INTO table_z (currentdate, months, days) VALUES (TO_DATE ('03/02/2006', 'MM/DD/YYYY'), 0, 300);
INSERT INTO table_z (currentdate, months, days) VALUES (TO_DATE ('04/05/2006', 'MM/DD/YYYY'), 10, 0);
INSERT INTO table_z (currentdate, months, days) VALUES (TO_DATE ('05/04/2006', 'MM/DD/YYYY'), 3, 0);I'm trying to add the given number of months and/or days to currentdate until I reach a given hard-coded cutoff date (05/01/2008 in this case).
That is, the desired results from the data above are:
CURRENTDAT MONTHS DAYS FINALDATE
03/02/2006 0 300 08/18/2008
04/05/2006 10 0 10/05/2008
05/04/2006 3 0 05/04/2008Edited by: Frank Kulash on May 4, 2009 7:38 PM
Typo in CONNECT BY conditions fixed.Hi Frank,
Sounds like a bug. Funny enough, but this works:
SQL> select * from v$version where rownum = 1
2 /
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
SQL> SELECT currentdate
2 , ADD_MONTHS ( currentdate
3 , (LEVEL - 1) * months
4 ) + ((LEVEL - 1) * days) AS final_date
5 , level
6 FROM table_z
7 WHERE CONNECT_BY_ISLEAF = 1
8 CONNECT BY currentdate = PRIOR currentdate
9 AND PRIOR dbms_random.value IS NOT NULL
10 AND ADD_MONTHS ( currentdate
11 , (LEVEL - 1) * months
12 ) + ((LEVEL - 1) * days) <= TO_DATE ( '05/01/2008'
13 , 'MM/DD/YYYY'
14 );
FROM table_z
ERROR at line 6:
ORA-01436: CONNECT BY loop in user data
SQL>
SQL> ed
Wrote file afiedt.buf
1 SELECT currentdate
2 , ADD_MONTHS ( currentdate
3 , (LEVEL - 1) * months
4 ) + ((LEVEL - 1) * days) AS final_date
5 , level
6 FROM table_z
7 WHERE CONNECT_BY_ISLEAF = 1
8 CONNECT BY currentdate = PRIOR currentdate + 0
9 AND PRIOR dbms_random.value IS NOT NULL
10 AND ADD_MONTHS ( currentdate
11 , (LEVEL - 1) * months
12 ) + ((LEVEL - 1) * days) <= TO_DATE ( '05/01/2008'
13 , 'MM/DD/YYYY'
14* )
SQL> /
CURRENTDA FINAL_DAT LEVEL
02-MAR-06 23-OCT-07 3
05-APR-06 05-DEC-07 3
04-MAY-06 04-FEB-08 8
SQL> SY. -
How to use dbms_random in TimesTen
Hi, All,
dbms_random is the supplied procedure in TimesTen 11.2.1,
but I failed with the following select statement:
select dbms_random.value from dual; or select sys.dbms_random.value from dual
the error I got is "1024: Referenced table DBMS_RANDOM not in FROM list"
But all of the query is OK for OracleDB.
How can I use dbms_random in TimesTen?
Thanks
Edited by: user536764 on 2011-1-5 上午8:24In pl/sql work fine
declare
v number;
begin
v := dbms_random.value(1,100);
dbms_output.put_line(v);
end;
85.95011054036622138729731389231348690891 -
I would like to return a number between 0-9999 through the dbms_random function. I am trying this:
exec dbms_random.initialize(1234);
select dbms_random.random from dual;
RANDOM
-3.10E+08
this is not what I am looking for. Anyone know how I can get the desired results I am looking for?SQL> SELECT DBMS_RANDOM.Value(0,9999) FROM dual;
DBMS_RANDOM.VALUE(0,9999)
2499.13527
SQL> /
DBMS_RANDOM.VALUE(0,9999)
6274.76354TTFN
John -
DBMS_RANDOM inconsistent behaviour ?
Hi:
I have an issue with DBMS_RANDOM on 9.2.0.6
#### TEST CASE #####
create table t1 (col1 number);
insert into t1 values ('1');
insert into t1 values ('2');
insert into t1 values ('3');
-- ## QUERY1
select rnd1, rnd1, col1 from (select * from (select dbms_random.string('U',5)
rnd1,col1 from t1));
RND1 RND1 COL1
<font color="#cc0000">EVJMP CZGSY</font> 1 <- the two occurrences of RND are different
WHZTP TEJBP 2
HOMYO DQVKG 3
-- ## QUERY2
select rnd1, rnd1, col1 from (select * from (select dbms_random.string('U',5)
rnd1,col1 from t1 where rownum < 99));
RND1 RND1 COL1
<font color="#0000cc">DGMSG DGMSG</font> 1 <- the two occurrences of RND are equal
MUEFU MUEFU 2
UAKLH UAKLH 3
##########<br>
I believe that query1 behaves wrong, I cannot reference the random value... while I believe query2 is correct.<br>
My goal is to have multiple occurrences of rnd1 field share the same value, without applying filtering with rownum.<br>
Is this the intended behaviour?<br>
Any idea on how to make query1 behave as query2?Very odd this is what you select returns on 9.2.0.6
SQL> select * from (select * from (select dbms_random.string('U',5)
2 rnd1,dbms_random.string('U',5)rnd2, col1 from t1 where rownum < 99))
3 /
RND1 RND2 COL1
AOPLL DHRHE 1
XPACR ADOUK 2
YIKOZ SERFB 3
And when I use
SQL> select rnd1, rnd1, col1
2 from (select rownum as Rnum,dbms_random.string('U',5) rnd1, col1 from t1)
3 /
RND1 RND1 COL1
CGXNN CGXNN 1
IYWMM IYWMM 2
GDTCT GDTCT 3
GJPLM GJPLM 4
JHXCO JHXCO 5
null -
Dbms_random distribution
What number distribution does the random number generator in the dbms_random package produce?
Michael Peddemors (guest) wrote:
: Is the DBMS_RANDOM package included in the 8.0.5 distribution?
: My calls to it are the typical..
: PLS-00201: identifier 'DBMS_RANDOM.INITIALIZE' must be declared
Okay, Thanks, found that $ORACLE_HOME/rdbms/admin/dbmsrand.sql
and ran it but.. it required the dbms_crypto_toolkit
So, I found dbmsoctk.sql but it has no function body
ORA-04068: existing state of packages has been discarded
ORA-04067: not executed, package body "SYS.DBMS_CRYPTO_TOOLKIT"
does not exist
ORA-06508: PL/SQL: could not find program unit being called
ORA-06512: at "SYS.DBMS_RANDOM", line 19
ORA-06512: at line 7
Did a grep of all the .sql files and found few bodies, EXCEPT in
the dbmsrand.sql...
But it requires this.. anyone??
null -
DBMS_RANDOM.Random
hi,
i want to see all the options available in DBMS_RANDOM package.
like the same how can i see from DBMS_LOB,AND other dbms packages.
is there any dictionary is available.
please advice.
thanks
sivasankar.sEverything you wanted to know about DBMS_XXXX packages...
http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14258/toc.htm
.... excerpt below....
DBMS_RANDOM
Using DBMS_RANDOM
Security Model
Operational Notes
Summary of DBMS_RANDOM Subprograms
INITIALIZE Procedure
NORMAL Function
RANDOM Procedure
SEED Procedures
STRING Function
TERMINATE Procedure
VALUE Functions Message was edited by:
Mark Reichman -
Dbms_random.value
SELECT value
into xxxx
FROM
( SELECT value FROM a
ORDER BY dbms_random.value )
WHERE rownum = 1;
When it picks value 123, how can code it to not pick value123 at least for a year?Hi,
When it picks value 123, how can code it to not pick value123 at least for a year?You're asking for a random value, and yet you want it to be (albeit temporarily) unique ?
bells ringin'
That's an oxymoron ;-)
See:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:831827028200 -
I use The ORACLE version is 8.05 and the OS is win2000 server.
I want to know execute DBMS_RANDOM, it must be installed what
associated packages? And before this i run the script
dbmsoctk.sql and dbmsrand.sql and grant execute privilege to the
user, Why when I Execute the procedure randomdata the ORACLE
tell me the package body "SYS.DBMS_CRYPTO_TOOLKIT" not exist?
Urgent! Pls help me!If you can find later versions of dbmsrand.sql, they don't
require that package. Or you could use
http://burtleburtle.net/bob/other/rnd.sql
which is similar to the later versions. -
Hi,
I got one suituation to calculate the percentage and based on that i have to do an insertion into tableA and tableB.
4 types of roles are there role1, role2, role3, role4 .
role1 80%
role2 70%
role3 30%
role4 10%
I have to generate random number, if the role -> role1 random number is 55 and its entered into tableA . next time role2 user is entered
and the random number is 20 and its entered into TableB.... But now my question is i have to use 4 random numbers .
is it possible to use in oracle procedure. i should not use any temp tables ... is there any other solutions are there ?
Regards,
KBGHaven't you read my last piece of code?
declare
rand_num_1 number := trunc(dbms_random.value(0,100));
rand_num_2 number := trunc(dbms_random.value(0,100));
rand_num_3 number := trunc(dbms_random.value(0,100));
rand_num_4 number := trunc(dbms_random.value(0,100));
begin
-- do what you want with the 4 random numbers...
end;
SQL> declare
2 rand_num_1 number := trunc(dbms_random.value(0,100));
3 rand_num_2 number := trunc(dbms_random.value(0,100));
4 rand_num_3 number := trunc(dbms_random.value(0,100));
5 rand_num_4 number := trunc(dbms_random.value(0,100));
6 begin
7 dbms_output.put_line('Rand1 ='||rand_num_1);
8 dbms_output.put_line('Rand2 ='||rand_num_2);
9 dbms_output.put_line('Rand3 ='||rand_num_3);
10 dbms_output.put_line('Rand4 ='||rand_num_4);
11 end;
12 /
Rand1 =2
Rand2 =55
Rand3 =58
Rand4 =99Max
[My Italian Oracle blog|http://oracleitalia.wordpress.com/2010/01/10/crittografia-in-plsql-utilizzando-dbms_crypto/]
Edited by: Massimo Ruocchio on Jan 12, 2010 12:40 PM
Added example -
How to generate DBMS_RANDOM Timestamp using procedure
Hi,
I got a copy from askTom site for the store procedure of creating random numbers in a table:
create or replace procedure clone( p_tname in
varchar2, p_records in number )
authid current_user
as
l_insert long;
l_rows number default 0;
begin
execute immediate 'create table clone_' || p_tname ||
' as select * from ' || p_tname ||
' where 1=0';
l_insert := 'insert into clone_' || p_tname ||
' select ';
for x in ( select data_type, data_length,
rpad( '9',data_precision,'9')/power(10,data_scale) maxval
from user_tab_columns
where table_name = 'CLONE_' || upper(p_tname)
order by column_id )
loop
if ( x.data_type in ('NUMBER', 'FLOAT' ))
then
l_insert := l_insert || 'dbms_random.value(1,' || x.maxval ||
elsif ( x.data_type = 'DATE' )
then
l_insert := l_insert ||
'sysdate+dbms_random.value+dbms_random.value(1,1000),';
else
l_insert := l_insert || 'dbms_random.string(''A'',' ||
x.data_length || '),';
end if;
end loop;
l_insert := rtrim(l_insert,',') ||
' from all_objects where rownum <= :n';
loop
execute immediate l_insert using p_records - l_rows;
l_rows := l_rows + sql%rowcount;
exit when ( l_rows >= p_records );
end loop;
end;
Can somebody tell me how to generate a random TIMESTAMP?
I try to change "sysdate" to systimestamp" and it give me an error:
ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
Please help!!
Message was edited by:
eecow
Message was edited by:
eecow
nullCan you give me an example of doing this?
The other question is:
Can the store procedure code (above) check other things other than data type?
For example:
if attributeName = 'percentage' --> create a random number from 0 to 100
The reason is because I am trying to generate some more "realistic" data. Some fields can be unrelistic, but fields like "percentage" should be within 0-100 and should not have something like 200 :)
Please advise.
Thanks! -
Hi all...
I am trying dbms_random like this :
**Type data :
AAAA VARCHAR2(35)
BBBB VARCHAR2(6)
DDDD VARCHAR2(20)
E_DATE VARCHAR2(14)
**store procedur :
BEGIN
FOR X IN 0..3 LOOP
INSERT INTO TEST VALUES (
'AAAA--'|| dbms_random.string('X', 28),
'BBBB' || TO_CHAR (TRUNC(dbms_random.value(0,10))),
'CCCC--'|| dbms_random.string('X', 12),
TO_DATE(TRUNC(DBMS_RANDOM.VALUE(2455532,2455563+3)),'J')
END LOOP;
END;
Output :
AAAA BBBB DDDD E_DATE
AAAA--RTL2OWN8RVOMY1IUD93RP91RWOQA BBBB1 CCCC--JCWAXRO3TMP0 12-DEC-10
AAAA--629F3A4AIW5E4OHCDVFZ41SFB2U4 BBBB5 CCCC--DLAUZO511Y97 12-DEC-10
AAAA--XHKR95JISQRTRH54KNOQKDVUJ6QF BBBB6 CCCC--K7CHOT2KULNV 10-DEC-10
AAAA--7DRZ38SPRIN26HNA7VPQV9FKWCCQ BBBB7 CCCC--FXV3CCY3BWHY 09-DEC-10i want format E_DATE to DD-MM-YYYY without change type data and still use dbms_random...
Could you pls solve me out ?
Thank you
-newbie-pl/sql-Hi,
Sorry, I'm not sure I uderstand the problem.
Are you getting the date correctly, but you want to store it in a VARCHAR2 column? That's not a very good idea: dates belong in DATE columns, but if you ever want to convert a DATE into a string, use TO_CHAR:
TO_CHAR ( TO_DATE ( TRUNC ( DBMS_RANDOM.VALUE (2455532, 2455563+3)
, 'J'
, 'DD-MM-YYYY'
)or equivalently:
TO_CHAR ( DATE '2010-12-01' + dbms_random.value (0, 34)
, 'DD-MM-YYYY'
)Since you're not displaying the hours, minutes or seconds, there's no point in using TRUNC just to set them to 00:00:00.
Edited by: Frank Kulash on Feb 9, 2011 10:32 PM
Maybe you are looking for
-
Macbook will not charge. Unit or adapter problem?
Last night the Macbook Pro power adapter would not light up or charge the Mac unless held in very specific position. Now apadter refuses to charge Macbook, regardless of position. Message was edited by: josephfromlynn Thank you. Issue resolved.
-
Mystery icon in Mailbox column headers
For no apparent reason, now when I create a new mailbox, the first column header contains a mystery icon consisting of two arrows, one pointing northwest and the other pointing southeast. When I right-click on the headers, there is no corresponding i
-
How do you keep your lens dry in dewy conditions
How do I keep my lens dry in dewy conditions
-
Context menu for tree node cannot be accessed in IE 8 (Jdev 11.1.2)
We use an af:tree with context menus for performing operations on each node. In IE 8 when you hover over a node before right clicking, "tooltip" text appears which basically just re-displays the text of the node. Even though the tooltip text is annoy
-
I use Crystal Reports built-in parameters ASP.NET prompt. Is it possible to set focus on first parameter (e.g. text box) if prompt web page appears ? Thanks.