原文地址:
oracle生成随机数大概有如下方法:
一:DBMS_RANDOM.NORMAL:官方文档上就说返回一个随机的数值,语法如下:
DBMS_RANDOM.NORMAL
RETURN NUMBER;
SCOTT@ORA11GR2>select DBMS_RANDOM.NORMAL from dual connect by rownum <=10;
NORMAL
----------
-1.3393161
.701245541
2.12367498
-.14406588
-1.7507766
-.30699373
-.96695265
-.46290516
-1.1982511
-.20578965
10 rows selected.
SCOTT@ORA11GR2>
从随便生成的10条记录来看,这个随机的数值,都是带有小数、有正有负且都不大的数值
二:DBMS_RANDOM.RANDOM 返回 [-2^^31, 2^^31)的整数,语法如下:
DBMS_RANDOM.RANDOM
RETURN binary_integer;
SCOTT@ORA11GR2>select DBMS_RANDOM.RANDOM from dual connect by rownum <=10;
RANDOM
----------
877380521
-224359155
408859296
319326341
-238184379
-9749965
452804816
-726968402
1896632203
1573700098
10 rows selected.
SCOTT@ORA11GR2>
这个官方文档解释的很明确,结果也如是。
三:DBMS_RANDOM.VALUE:返回 [0,1) 之间38位的小数,语法如下:
DBMS_RANDOM.VALUE
RETURN NUMBER;
SCOTT@ORA11GR2>select DBMS_RANDOM.VALUE from dual connect by rownum<=10;
VALUE
----------
.00808596
.343671946
.025661757
.89370666
.278816137
.665851998
.958039013
.080123964
.612433842
.031469894
10 rows selected.
SCOTT@ORA11GR2>
四:DBMS_RANDOM.VALUE:返回参数low和参数high之间的整数,语法如下:
DBMS_RANDOM.VALUE(
low IN NUMBER,
high IN NUMBER)
RETURN NUMBER;
SCOTT@ORA11GR2>select DBMS_RANDOM.VALUE(1,20) from dual connect by rownum<=10;
DBMS_RANDOM.VALUE(1,20)
-----------------------
2.49387333
13.3879103
5.8981475
2.53665301
8.43253438
10.8722281
12.4991216
8.63369118
5.06535416
4.02958153
10 rows selected.
SCOTT@ORA11GR2>
五:DBMS_RANDOM.STRING 返回随机字符串,语法如下:
DBMS_RANDOM.STRING
opt IN CHAR,
len IN NUMBER)
RETURN VARCHAR2;
---------------------------------------------
参数说明:
参数1):opt
'u','U' - 返回字符串的大写字母字符
'l','L' - 返回字符串中的小写字母字符
'a','A' - 返回字符串大小写混合的字母字符
'x','X' - 返回字符串的大写字母数字字符
'p','P' - 返回字符串中任何可打印字符。
否则,返回的字符串是大写字母字符。
参数2):len 返回字符串长度
---------------------------------------------
SCOTT@ORA11GR2>select DBMS_RANDOM.STRING('u',20) from dual;
DBMS_RANDOM.STRING('U',20)
------------------------------------------------------
SLAFBJKBQHCXQNSCAQUP
SCOTT@ORA11GR2>select DBMS_RANDOM.STRING('L',20) from dual;
DBMS_RANDOM.STRING('L',20)
------------------------------------------------------
urzfykwqhmvrnqgvazea
SCOTT@ORA11GR2>select DBMS_RANDOM.STRING('a',20) from dual;
DBMS_RANDOM.STRING('A',20)
------------------------------------------------------
tHEnppmOkJFUEKSirusR
SCOTT@ORA11GR2>select DBMS_RANDOM.STRING('x',20) from dual;
DBMS_RANDOM.STRING('X',20)
------------------------------------------------------
U8P5XDPMIW4WRVBLHQHN
SCOTT@ORA11GR2>select DBMS_RANDOM.STRING('p',20) from dual;
DBMS_RANDOM.STRING('P',20)
------------------------------------------------------
V{!Da1?@Q*.q4t1kOkE=
SCOTT@ORA11GR2>select DBMS_RANDOM.STRING('z',20) from dual;
DBMS_RANDOM.STRING('Z',20)
------------------------------------------------------
SWEMBLZOYVIKWWSNKAML
SCOTT@ORA11GR2>
六:SYS_GUID():生成一个全局唯一的、32位的字符串,有点儿类似java中的uuid
SCOTT@ORA11GR2>select SYS_GUID() from dual;
SYS_GUID()
--------------------------------
C95A782E9EF11062E040A8C0C8017914
SCOTT@ORA11GR2>
小结:
随机数经常用于生成密码或者随机排序,比如:在sql语句中,我们就可以order by DBMS_RANDOM.RANDOM,如下:
SCOTT@ORA11GR2>select rownum from dual connect by rownum<=10 order by DBMS_RANDOM.RANDOM;
ROWNUM
----------
8
6
1
3
9
7
4
10
5
2
10 rows selected.
SCOTT@ORA11GR2>