Oracle随机函数之dbms_random使用详解

前端之家收集整理的这篇文章主要介绍了Oracle随机函数之dbms_random使用详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

dbms_random是oracle提供的一个随机函数包,以下介绍一些dbms_random的常用示例:
dbms_random.value用法
生成一个大于等于0,小于等于1的38位小数
<div class="codetitle"><a style="CURSOR: pointer" data="70057" class="copybut" id="copybut70057" onclick="doCopy('code70057')"> 代码如下:

<div class="codebody" id="code70057">
-- FUNCTION value RETURN NUMBER;
select dbms_random.value from dual;
sql> select dbms_random.value from dual;
VALUE
----------
0.61011338

<div class="codetitle"><a style="CURSOR: pointer" data="70057" class="copybut" id="copybut70057" onclick="doCopy('code70057')"> 代码如下:
<div class="codebody" id="code70057">
-- FUNCTION value RETURN NUMBER;
select dbms_random.value from dual;
sql> select dbms_random.value from dual;
VALUE
----------
0.61011338

生成一个指定范围内的数</ p>
<div class="codetitle"><a style="CURSOR: pointer" data="39745" class="copybut" id="copybut39745" onclick="doCopy('code39745')"> 代码如下:
<div class="codebody" id="code39745">
select dbms_random.value(100,0)
from dual;
sql> select dbms_random.value(100,0)
2 from dual;
DBMS_RANDOM.VALUE(100,0)
------------------------
20.7742244285517

<div class="codetitle"><a style="CURSOR: pointer" data="79265" class="copybut" id="copybut79265" onclick="doCopy('code79265')"> 代码如下:
<div class="codebody" id="code79265">
-- FUNCTION value (low IN NUMBER,high IN NUMBER) RETURN NUMBER;
select dbms_random.value(100,0)
------------------------
20.7742244285517

dbms_random.normal用法
获取正态分布的随机
<div class="codetitle"><a style="CURSOR: pointer" data="60703" class="copybut" id="copybut60703" onclick="doCopy('code60703')"> 代码如下:<div class="codebody" id="code60703">
select dbms_random.normal from dual;
sql> select dbms_random.normal from dual;
NORMAL
----------
-1.7330759

<div class="codetitle"><a style="CURSOR: pointer" data="60703" class="copybut" id="copybut60703" onclick="doCopy('code60703')"> 代码如下:<div class="codebody" id="code60703">
select dbms_random.normal from dual;
sql> select dbms_random.normal from dual;
NORMAL
----------
-1.7330759

dbms_random.string用法
获取指定字符串
<div class="codetitle"><a style="CURSOR: pointer" data="69652" class="copybut" id="copybut69652" onclick="doCopy('code69652')"> 代码如下:<div class="codebody" id="code69652">
/ "opt" specifies that the returned string may contain:
'u','U' : upper case alpha characters only
'l','L' : lower case alpha characters only
'a','A' : alpha characters only (mixed case)
'x','X' : any alpha-numeric characters (upper)
'p','P' : any printable characters
/
sql>
select
dbms_random.string('u',10)
from dual
union all
select
dbms_random.string('U',10)
from dual
union all
select
dbms_random.string('l',10)
from dual
union all
select
dbms_random.string('L',10)
from dual
union all
select
dbms_random.string('a',10)
from dual
union all
select
dbms_random.string('A',10)
from dual
union all
select
dbms_random.string('x',10)
from dual
union all
select
dbms_random.string('X',10)
from dual
union all
select
dbms_random.string('P',10)
from dual;

<div class="codetitle"><a style="CURSOR: pointer" data="89542" class="copybut" id="copybut89542" onclick="doCopy('code89542')"> 代码如下:<div class="codebody" id="code89542">
--FUNCTION string (opt char,len NUMBER)
/* "opt" specifies that the returned string may contain:
'u',10)
from dual;

DBMS_RANDOM.STRING(‘U',10)
—————————-
TXREHAICRI
VDTMXZORVB
udavjpudfb
hvfqhjjdgz
tZoanQzxtX
siATLEZXQa
2LWWZ3H3L5
ZF6MKKG1R7
#\j5IPva(W
sJe/srX:ZB
10 rows selected
dbms_random.seed用法
–可以设置seed来确定随机数的起始点,对于相同的seed而言,随机数的任意一次变化都将是确定的。
– 就是说,如果在某一时刻调用了seed,之后第一次产生的随机数是4,第二次是6,第三次是1,
– 那么当你再次调用相同的seed之后,一次产生的随机数还是4、6、1
– seed有两种,一种是数值型的,一种是字符型(最大长度2000)的
<div class="codetitle"><a style="CURSOR: pointer" data="56430" class="copybut" id="copybut56430" onclick="doCopy('code56430')"> 代码如下:<div class="codebody" id="code56430">
SELECT USERENV('SESSIONID')
FROM DUAL;
BEGIN
dbms_random.seed(6);
END;
/
SELECT DBMS_RANDOM.value
FROM DUAL
CONNECT BY LEVEL < 10;

<div class="codetitle"><a style="CURSOR: pointer" data="56430" class="copybut" id="copybut56430" onclick="doCopy('code56430')"> 代码如下:<div class="codebody" id="code56430">
SELECT USERENV('SESSIONID')
FROM DUAL;
BEGIN
dbms_random.seed(6);
END;
/
SELECT DBMS_RANDOM.value
FROM DUAL
CONNECT BY LEVEL < 10;

--SESSION 1
<div class="codetitle"><a style="CURSOR: pointer" data="31200" class="copybut" id="copybut31200" onclick="doCopy('code31200')"> 代码如下:<div class="codebody" id="code31200">
sql> SELECT USERENV('SESSIONID')
2 FROM DUAL;
USERENV('SESSIONID')
--------------------
15140521
sql> BEGIN
2 dbms_random.seed(100);
3 END;
4 /
PL/sql procedure successfully completed
sql> SELECT DBMS_RANDOM.value
2 FROM DUAL
3 CONNECT BY LEVEL < 10;
VALUE
----------
0.53801770
0.67499536
0.65362270
0.76351985
0.29859834
0.40522032
0.99551636
0.39565580
0.18074760
9 rows selected

<div class="codetitle"><a style="CURSOR: pointer" data="31200" class="copybut" id="copybut31200" onclick="doCopy('code31200')"> 代码如下:<div class="codebody" id="code31200">
sql> SELECT USERENV('SESSIONID')
2 FROM DUAL;
USERENV('SESSIONID')
--------------------
15140521
sql> BEGIN
2 dbms_random.seed(100);
3 END;
4 /
PL/sql procedure successfully completed
sql> SELECT DBMS_RANDOM.value
2 FROM DUAL
3 CONNECT BY LEVEL < 10;
VALUE
----------
0.53801770
0.67499536
0.65362270
0.76351985
0.29859834
0.40522032
0.99551636
0.39565580
0.18074760
9 rows selected

--SESSION 2
<div class="codetitle"><a style="CURSOR: pointer" data="57239" class="copybut" id="copybut57239" onclick="doCopy('code57239')"> 代码如下:<div class="codebody" id="code57239">
sql> SELECT USERENV('SESSIONID')
2 FROM DUAL;
USERENV('SESSIONID')
--------------------
15140517
sql> BEGIN
2 dbms_random.seed(100);
3 END;
4 /
PL/sql procedure successfully completed
sql> SELECT DBMS_RANDOM.value
2 FROM DUAL
3 CONNECT BY LEVEL < 10;
VALUE
----------
0.53801770
0.67499536
0.65362270
0.76351985
0.29859834
0.40522032
0.99551636
0.39565580
0.18074760
9 rows selected

<div class="codetitle"><a style="CURSOR: pointer" data="57239" class="copybut" id="copybut57239" onclick="doCopy('code57239')"> 代码如下:<div class="codebody" id="code57239">
sql> SELECT USERENV('SESSIONID')
2 FROM DUAL;
USERENV('SESSIONID')
--------------------
15140517
sql> BEGIN
2 dbms_random.seed(100);
3 END;
4 /
PL/sql procedure successfully completed
sql> SELECT DBMS_RANDOM.value
2 FROM DUAL
3 CONNECT BY LEVEL < 10;
VALUE
----------
0.53801770
0.67499536
0.65362270
0.76351985
0.29859834
0.40522032
0.99551636
0.39565580
0.18074760
9 rows selected

原文链接:https://www.f2er.com/oracle/65624.html
dbms_randomoracleoracle

猜你在找的Oracle相关文章