zoukankan      html  css  js  c++  java
  • Oracle随机函数的取法

    简单得说,通过dbms_random包调用随机数的方法大致有4种:

    1、dbms_random.normal

    这个函数不带参数,能返回normal distribution的一个number类型,所以基本上随机数会在-1到1之间。

        简单测试了一下,产生100000次最大能到5左右:

        SQL> declare
          2    i number:=0;
          3    j number:=0;
          4 begin
          5    for k in 1 .. 100000 loop
          6    i:= dbms_random.normal;
          7      if i > j
          8        then j:=i;
          9      end if;
          10    end loop;
          11    dbms_output.put_line(j);
          12 end;
          13 /

        5.15325081797418404136433867107468983182

        PL/SQL procedure successfully completed

    2、dbms_random.random

        这个也没有参数,返回一个从-power(2,31)到power(2,31)的整数值

    3、dbms_random.value

        这个函数分为两种,一种是没有参数,则直接返回0-1之间的38位小数

        SQL > column value format 9.99999999999999999999999999999999999999
        SQL > select dbms_random.value from dual;

                                            VALUE
        -----------------------------------------
          .58983014999643548701631750396301271752

        第二种是加上两个参数a、b,则返回值在a、b之间的38位小数

        SQL > column value format 999.999999999999999999999999999999999999
        SQL > select dbms_random.value(100,500) value from dual;

                                            VALUE
        -----------------------------------------
          412.150194612502916808701157054098274240

        注意:无论前面几位,小数点之后都是38位

    4、dbms_random.string

        这个函数必须带有两个参数,前面的字符指定类型,后面的数值指定位数(最大60)

        类型说明:

        '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 > column value format a30
        SQL > select dbms_random.string('u',30) value from dual;

        VALUE
        ------------------------------
        VTQNLGISELPXEDBXKUZLXKBAJMUTIA

        SQL > select dbms_random.string('l',30) value from dual;

        VALUE
        ------------------------------
        uqygsbquingfqdytpgjvdoblxeglgu

        SQL > select dbms_random.string('a',30) value from dual;

        VALUE
        ------------------------------
        NGTGkQypuSWhBfcrHiOlQwOUXkqJjy

        SQL > select dbms_random.string('x',30) value from dual;

        VALUE
        ------------------------------
        UVWONYJMXT31VEFPD736WJCJ5QT6BD

        SQL > select dbms_random.string('p',30) value from dual;

        VALUE
        ------------------------------
        :mak$(WT4M_7c/+f[_XUscf$P Zcq{

    5、关于seed

        可以设置seed来确定随机数的起始点,对于相同的seed而言,随机数的任意一次变化都将是确定的。

        就是说,如果在某一时刻调用了seed,之后第一次产生的随机数是4,第二次是6,第三次是1,那么当你再次调用相同的seed之后,一次产生的随机数还是4、6、1

        seed有两种,一种是数值型的,一种是字符型(最大长度2000)的

        -- Seed with a binary integer

        PROCEDURE seed(val IN BINARY_INTEGER );

        PRAGMA restrict_references (seed, WNDS );

        -- Seed with a string (up to length 2000)

        PROCEDURE seed(val IN VARCHAR2 );

        PRAGMA restrict_references (seed, WNDS );

    6、关于initialize

        一个integer参数,注释说的很清楚了:

        -- Obsolete, just calls seed(val)

        PROCEDURE initialize(val IN BINARY_INTEGER );

        PRAGMA restrict_references (initialize, WNDS );

    sys_guid()

        官方文档的说明如下:


        SYS_GUID generates and returns a globally unique identifier (RAW value) made up of 16 bytes. On most platforms, the generated identifier consists of a host identifier, a process or thread identifier of the process or thread invoking the function, and a nonrepeating value (sequence of bytes) for that process or thread.

        简单得说就是,随机生成一个32位的RAW,但是后面的那段经过实验发现不是这么回事,每次生成的字符串都千差万别,不知道为什么。

        在具体应用中,除了可以用来插入生成唯一的标识符外,还可以用来取表中的任意一条记录:

        select * from ( select * from t2 order by sys_guid()) where rownum = 1 ;

  • 相关阅读:
    在OC和Swift中使用IBDesignable/IBInspectable
    Swift之贪婪的UIButton
    iOS:如何通过UIEdgeInsetsMake来制作可伸缩的Button
    iOS8中如何将状态栏的字体颜色改为白色
    iOS7 StatusBar 使用小结
    IOS 怎么修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
    android采用videoView播放视频(包装)
    面向对象设计——通用愉快的经历
    OCP-1Z0-051-名称解析-文章12称号
    图片切割工具---产生多个div切割图片 采用for和一的二维阵列设置背景位置
  • 原文地址:https://www.cnblogs.com/wuyisky/p/1611693.html
Copyright © 2011-2022 走看看