Oracle随机函数之dbms_random使用详解
程序员文章站
2023-12-02 08:05:46
dbms_random是oracle提供的一个随机函数包,以下介绍一些dbms_random的常用示例: dbms_random.value用法: 生成一个大于等于0,小于...
dbms_random是oracle提供的一个随机函数包,以下介绍一些dbms_random的常用示例:
dbms_random.value用法:
生成一个大于等于0,小于等于1的38位小数
-- function value return number;
select dbms_random.value from dual;
sql> select dbms_random.value from dual;
value
----------
0.61011338
-- function value return number;
select dbms_random.value from dual;
sql> select dbms_random.value from dual;
value
----------
0.61011338
生成一个指定范围内的数</ p>
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
-- function value (low in number, high in number) return number;
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
dbms_random.normal用法
获取正态分布的随机数
select dbms_random.normal from dual;
sql> select dbms_random.normal from dual;
normal
----------
-1.7330759
select dbms_random.normal from dual;
sql> select dbms_random.normal from dual;
normal
----------
-1.7330759
dbms_random.string用法
获取指定字符串
/* "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
union all
select
dbms_random.string('p',10)
from dual;
--function string (opt char, len number)
/* "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
union all
select
dbms_random.string('p',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)的
select userenv('sessionid')
from dual;
begin
dbms_random.seed(6);
end;
/
select dbms_random.value
from dual
connect by level < 10;
select userenv('sessionid')
from dual;
begin
dbms_random.seed(6);
end;
/
select dbms_random.value
from dual
connect by level < 10;
--session 1
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
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
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
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
dbms_random.value用法:
生成一个大于等于0,小于等于1的38位小数
复制代码 代码如下:
-- function value return number;
select dbms_random.value from dual;
sql> select dbms_random.value from dual;
value
----------
0.61011338
复制代码 代码如下:
-- function value return number;
select dbms_random.value from dual;
sql> select dbms_random.value from dual;
value
----------
0.61011338
生成一个指定范围内的数</ p>
复制代码 代码如下:
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
复制代码 代码如下:
-- function value (low in number, high in number) return number;
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
dbms_random.normal用法
获取正态分布的随机数
复制代码 代码如下:
select dbms_random.normal from dual;
sql> select dbms_random.normal from dual;
normal
----------
-1.7330759
复制代码 代码如下:
select dbms_random.normal from dual;
sql> select dbms_random.normal from dual;
normal
----------
-1.7330759
dbms_random.string用法
获取指定字符串
复制代码 代码如下:
/* "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
union all
select
dbms_random.string('p',10)
from dual;
复制代码 代码如下:
--function string (opt char, len number)
/* "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
union all
select
dbms_random.string('p',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)的
复制代码 代码如下:
select userenv('sessionid')
from dual;
begin
dbms_random.seed(6);
end;
/
select dbms_random.value
from dual
connect by level < 10;
复制代码 代码如下:
select userenv('sessionid')
from dual;
begin
dbms_random.seed(6);
end;
/
select dbms_random.value
from dual
connect by level < 10;
--session 1
复制代码 代码如下:
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
复制代码 代码如下:
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
复制代码 代码如下:
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
复制代码 代码如下:
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
推荐阅读
-
Oracle随机函数之dbms_random使用详解
-
oracle合并列的函数wm_concat的使用详解
-
mysql踩坑之limit与sum函数混合使用问题详解
-
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
-
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
-
Oracle随机函数之dbms_random使用详解
-
oracle合并列的函数wm_concat的使用详解
-
php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
-
javascript如何使用函数random来实现课堂随机点名方法详解
-
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】