Oracle随机函数

 dbms_random 

 

CREATE OR REPLACE PACKAGE SYS.dbms_random AS

 

    ------------

    --  OVERVIEW

    --

    --  This package should be installed as SYS.  It generates a sequence of

    --  random 38-digit Oracle numbers.  The expected length of the sequence

    --  is about power(10,28), which is hopefully long enough.

    --

    --------

    --  USAGE

    --

    --  This is a random number generator.  Do not use for cryptography.

    --  For more options the cryptographic toolkit should be used.

    --

    --  By default, the package is initialized with the current user

    --  name, current time down to the second, and the current session.

    --

    --  If this package is seeded twice with the same seed, then accessed

    --  in the same way, it will produce the same results in both cases.

    --

    --------

    --  EXAMPLES

    --

    --  To initialize or reset the generator, call the seed procedure as in:

    --      execute dbms_random.seed(12345678);

    --    or

    --      execute dbms_random.seed(TO_CHAR(SYSDATE,'MM-DD-YYYY HH24:MI:SS'));

    --  To get the random number, simply call the function, e.g.

    --      my_random_number BINARY_INTEGER;

    --      my_random_number := dbms_random.random;

    --    or

    --      my_random_real NUMBER;

    --      my_random_real := dbms_random.value;

    --  To use in SQL statements:

    --      select dbms_random.value from dual;

    --      insert into a values (dbms_random.value);

    --      variable x NUMBER;

    --      execute :x := dbms_random.value;

    --      update a set a2=a2+1 where a1 < :x;

 

    -- 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 );

 

    -- Get a random 38-digit precision number, 0.0 <= value < 1.0

    FUNCTION value RETURN NUMBER ;

    PRAGMA restrict_references ( value , WNDS );

 

    -- get a random Oracle number x, low <= x < high

    FUNCTION value (low IN NUMBER , high IN NUMBER ) RETURN NUMBER ;

    PRAGMA restrict_references ( value , WNDS );

 

    -- get a random number from a normal distribution

    FUNCTION normal RETURN NUMBER ;

    PRAGMA restrict_references (normal, WNDS );

 

    -- get a random string

    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

          */

        RETURN VARCHAR2 -- string of <len> characters (max 60)

    PRAGMA restrict_references ( string , WNDS );

 

    -- Obsolete, just calls seed(val)

    PROCEDURE initialize(val IN BINARY_INTEGER );

    PRAGMA restrict_references (initialize, WNDS );

 

    -- Obsolete, get integer in ( -power(2,31) <= random < power(2,31) )

    FUNCTION random RETURN BINARY_INTEGER ;

    PRAGMA restrict_references (random, WNDS );

 

    -- Obsolete, does nothing

    PROCEDURE terminate;

 

    TYPE num_array IS TABLE OF NUMBER INDEX BY BINARY_INTEGER ;

END dbms_random;

 

 

    简单得说,通过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位小数

 

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值