Oracle dbms
Jun 07, 2016 pm 03:50 PMOracle dbms_random包的用法 Oracle dbms_random包的用法 1.dbms_random.value方法 dbms_random是一个可以生成随机数或者字符串的程序包。这个包有initialize()、seed()、terminate()、value()、normal()、random()、string()等几个函数,但value()是最常用
Oracle dbms_random包的用法
Oracle dbms_random包的用法1.dbms_random.value方法
dbms_random是一个可以生成随机数值或者字符串的程序包。这个包有initialize()、seed()、terminate()、value()、normal()、random()、string()等几个函数,但value()是最常用的,value()的用法一般有两个种,第一
function value return number;
这种用法没有参数,会返回一个具有38位精度的数值,范围从0.0到1.0,但不包括1.0,如下示例:
SQL> set serverout on
SQL> begin
2 for i in 1..10 loop
3 dbms_output.put_line(round(dbms_random.value*100));
4 end loop;
5 end;
6 /
46
19
45
37
33
57
61
20
82
8
PL/SQL 过程已成功完成。
SQL>
第二种value带有两个参数,第一个指下限,第二个指上限,将会生成下限到上限之间的数字,但不包含上限,“学无止境”兄说的就是第二种,如下:
SQL> begin
2 for i in 1..10 loop
3 dbms_output.put_line(trunc(dbms_random.value(1,101)));
4 end loop;
5 end;
6 /
97
77
13
86
68
16
55
36
54
46
PL/SQL 过程已成功完成。
2. dbms_random.string 方法
某些用户管理程序可能需要为用户创建随机的密码。使用10G下的dbms_random.string 可以实现这样的功能。
例如:
SQL> select dbms_random.string('P',8 ) from dual ;
DBMS_RANDOM.STRING('P',8)
--------------------------------------------------------------------------------
3q
第一个参数的含义:
■ 'u', 'U' - returning string in uppercase alpha characters
■ 'l', 'L' - returning string in lowercase alpha characters
■ 'a', 'A' - returning string in mixed case alpha characters
■ 'x', 'X' - returning string in uppercase alpha-numeric
characters
■ 'p', 'P' - returning string in any printable characters.
Otherwise the returning string is in uppercase alpha
characters.
P 表示 printable,即字符串由任意可打印字符构成
而第二个参数表示返回的字符串长度。
3. dbms_random.random 方法
random返回的是BINARY_INTEGER类型值,产生一个任意大小的随机数
与dbms_random.value 的区别举例:
Order By dbms_random.value;
这条语句功能是实现记录的随机排序
另外:
dbms_random.value 和
dbms_random.random 两者之间有什么区别?
1。Order By dbms_random.value ,为结果集的每一行计算一个随机数,dbms_random.value 是结果集的一个列(虽然这个列并不在select list 中),然后根据该列排序,得到的顺序自然就是随机的啦。
2。看看desc信息便知道vlue和random这两个函数的区别了,value返回的是number类型,并且返回的值介于1和0之间,而random返回的是BINARY_INTEGER类型(以二进制形式存储的数字,据说运算的效率高于number但我没测试过,但取值范围肯定小于number,具体限制得查资料了)
如果你要实现随机排序,还是用value函数吧
4. dbms_random.normal方法
NORMAL函数返回服从正态分布的一组数。此正态分布标准偏差为1,期望值为0。这个函数返回的数值中有68%是介于-1与+1之间,95%介于-2与+2之间,99%介于-3与+3之间。
5. dbms_random.send方法
用于生成一个随机数种子,设置种子的目的是可以重复生成随机数,用于调试。否则每次不同,难以调度。
http://blog.csdn.net/skywolf2002/archive/2007/10/10/1818794.aspx
附加:SQL>select rownum as id,trunc(dbms_random.value(1,4)) as bitcol from dba_objects where rownum
ID BITCOL
---------- ----------
1 3
2 2
3 1
4 3
5 3
6 1
7 1
8 2
9 3
10 2
出自《bitmap索引的深入研究(自我改版)》

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Function to calculate the number of days between two dates in oracle

How long will Oracle database logs be kept?

The order of the oracle database startup steps is

How to see the number of occurrences of a certain character in Oracle

How to determine whether two strings are contained in oracle

Oracle database server hardware configuration requirements
