• 随机数


    1.创建下面的包
    CREATE OR REPLACE PACKAGE random IS
    
      -- Returns random integer between [0, r-1]
      FUNCTION rndint(r IN NUMBER) RETURN NUMBER;
    
      -- Returns random real between [0, 1]
      FUNCTION rndflt RETURN NUMBER;
    
    END;
    /
    
    CREATE OR REPLACE PACKAGE BODY random IS
    
      m         CONSTANT NUMBER:=100000000;  /* initial conditions */
      m1        CONSTANT NUMBER:=10000;      /* (for best results) */
      b         CONSTANT NUMBER:=31415821;   /*      */
      a         NUMBER;                      /* seed */
      the_date  DATE;                        /*      */
      days      NUMBER;                      /* for generating initial seed */
      secs      NUMBER;                      /*      */
    
      -- ------------------------
      -- Private utility FUNCTION
      -- ------------------------
      FUNCTION mult(p IN NUMBER, q IN NUMBER) RETURN NUMBER IS
        p1     NUMBER; 
        p0     NUMBER; 
        q1     NUMBER; 
        q0     NUMBER; 
      BEGIN 
        p1:=TRUNC(p/m1); 
        p0:=MOD(p,m1); 
        q1:=TRUNC(q/m1); 
        q0:=MOD(q,m1); 
        RETURN(MOD((MOD(p0*q1+p1*q0,m1)*m1+p0*q0),m)); 
      END;
    
      -- ---------------------------------------
      -- Returns random integer between [0, r-1]
      -- ---------------------------------------
      FUNCTION rndint (r IN NUMBER) RETURN NUMBER IS 
      BEGIN 
        -- Generate a random NUMBER, and set it to be the new seed
        a:=MOD(mult(a,b)+1,m); 
    
        -- Convert it to integer between [0, r-1] and return it
        RETURN(TRUNC((TRUNC(a/m1)*r)/m1));
      END;
     
      -- ----------------------------------
      -- Returns random real between [0, 1]
      -- ----------------------------------
      FUNCTION rndflt RETURN NUMBER IS
        BEGIN
          -- Generate a random NUMBER, and set it to be the new seed
          a:=MOD(mult(a,b)+1,m);
          RETURN(a/m);
        END;
    
    BEGIN
      -- Generate initial seed "a" based on system date
      the_date:=SYSDATE;
      days:=TO_NUMBER(TO_CHAR(the_date, 'J'));
      secs:=TO_NUMBER(TO_CHAR(the_date, 'SSSSS'));
      a:=days*24*3600+secs;
    END;
    /
    2.使用方法:
     
    得到指定范围内的随机数(0--r-1): 
    SQL> select random.rndint(100000)  from dual;   ---括号内指定最大值
    
    RANDOM.RNDINT(100000)
    ---------------------
                    18150
    得到一个随机的小数(0-1):
    select random.rndflt  from dual;


  • 相关阅读:
    nginx last break等
    Jmeter
    nginx location规则
    解决性能瓶颈的一些思路
    spring 一些总结
    idea快捷键
    可读的jvm gc日志时间
    redis 搭建集群
    windows下使用Python来修改文件时间戳
    Python获得文件时间戳
  • 原文地址:https://www.cnblogs.com/haoxiaoyu/p/3598709.html
Copyright © 2020-2023  润新知