从zyz2表中随机取出两条纪录:
SQL> select * from zyz2;
       ID         P1         P2
---------- ---------- ----------
        1        211        212
        2         12         14
        4        241        242
        2        145
        1         11
        3         17          26 rows selected.Elapsed: 00:00:00.50
SQL> select id,p1,p2 from (select zyz2.*,dbms_random.random num from zyz2 order
by num) where rownum<3;       ID         P1         P2
---------- ---------- ----------
        2         12         14
        1        211        212Elapsed: 00:00:00.30
SQL> select id,p1,p2 from (select zyz2.*,dbms_random.random num from zyz2 order
by num) where rownum<3;       ID         P1         P2
---------- ---------- ----------
        3         17          2
        2         12         14

解决方案 »

  1.   

    set serveroutput on;declare
      todttm   char(14);
      rndinit  number;
      rnd      number;
      keta     number := 5;  --随机数位数
    begin  todttm := to_char(sysdate, 'yyyymmddhh24miss');
      rndinit := to_number(substr(todttm, 1, 8)) + to_number(substr(todttm, 9, 6));  dbms_random.initialize(rndinit);  dbms_random.seed(rndinit);  rnd := to_number(substr(to_char(dbms_random.random()), - keta, keta));
      dbms_output.put_line ('-- 处理结果 --');
      dbms_output.put_line ('随机数1 = ' || rnd);  rnd := to_number(substr(to_char(dbms_random.random()), - keta, keta));
      dbms_output.put_line ('随机数2 = ' || rnd);  rnd := to_number(substr(to_char(dbms_random.random()), - keta, keta));
      dbms_output.put_line ('随机数3 = ' || rnd);  dbms_random.terminate;end;
    /
      

  2.   

    楼上两位都详细阐述了dbms_random内置函数的使用方法。如果需要精确的控制隋机数范围,例如(1-168),(256-4038)。。等等。用random实现似乎就不是这样容易了。
      

  3.   

    dbms_random.value(min_value,max_value)