AI智能
改变未来

探索各种随机函数(Java环境)

1.Wichman-Hill 随机数产生器

Excel的随机函数曾用的方法,参考文献:

Wichman, B.A. 和 I.D. Hill,Algorithm AS 183:An Efficient and Portable Pseudo-Random Number Generator,《Applied Statistics》,31,188-190,1982。 Wichman, B.A. 和 I.D. Hill,Building a Random-Number Generator,BYTE,第127-128 页,1987 年 3 月。 Rotz, W. 和 E. Falk,D. Wood 和 J. Mulrow,A Comparison of Random Number Generators Used in Business,发表于 2001 年在佐治亚州亚特兰大市举行的“统计学联合会议”上。

/*** This is a method of Wichman-Hill random number generator.** @param x* A seed for generator.* @return A float random value between [0.0,1.0)*/public static float randomWH(java.lang.Integer x) {   int[] seed = new int[3];seed[0] = (171 * x) % 30269;seed[1] = (172 * (30000 – x)) % 30307;seed[2] = (170 * x) % 30323;   return (x / Math.abs(x))* (seed[0] / 30269.0F + seed[1] / 30307.0F + seed[2] / 30323.0F) % 1.0F;}

[[1, 0.26327664], [2, 0.26885703], [3, 0.27443743], [4, 0.28001782], [5, 0.28559822], [6, 0.29117858], [7, 0.296759], [8, 0.30233938], [9, 0.3079198], [10, 0.31350017], [11, 0.31908056], [12, 0.324661], [13, 0.33024135], [14, 0.33582175], [15, 0.34140214]] 平均时间很神奇的都是0-1ms,我的天哪里错了吗?

2.RSA 随机数产生器

RSA公钥算法大家都不会不熟悉吧,公认很靠谱的密钥算法。这里就是用了RSA的随机算法。参考:

Wikipedia – RSA problem 其公式:C = (x * exp P) mod N(P是质数,N是两个质数之积)

/*** This is a method of RSA.** @param x*            A seed for generator.* @return A float random value between [0.0,1.0)*/public static float randomRSA(java.lang.Integer x) {    return (float) (x * Math.exp(seedRSA[0]) % seedRSA[1] / seedRSA[1]);}

3.Java 随机数产生器

Java自带的随机数(就是java.util.Random类),用过的都知道吧。那就直接上代码:

/*** This is a method of Java random number generator.** @param x*            A seed for generator.* @return A float random value between [0.0,1.0)*/public static float randomJava(java.lang.Integer x) {    return (float) (new java.util.Random(1000 * x).nextDouble()); //乘1000来让种子间差距增大}

[1,0.71018493],[2,0.6234702],[3,0.900814],[4,0.80836606],[5,0.08570986],[6,0.9760623],[7,0.25340614],[8,0.16095819],[9,0.97060144],[10,0.8838867],[11,0.13829768],[12,0.045849733],[13,0.32319355],[14,0.23647882],[15,0.5138226] 平均时间:0-1ms

4.简单的随机数产生器

又是扫荡Google的战利品,很抱歉忘记出处惹……代码:

/*** This is a method of basic random generator.** @param x*            A seed for generator.* @return A float random value between [0.0,1.0)*/public static float randomBasic(java.lang.Integer x) {x = (x << 13) ^ x;    return (float) Math.abs((1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0));}

[1,0.22637305],[2,0.2936329],[3,0.2571852],[4,0.5857607],[5,0.71294135],[6,0.31129658],[7,0.123062514],[8,0.8130556],[9,0.03246965],[10,0.92940825],[11,0.5371662],[12,0.7446079],[13,0.45132604],[14,0.11840015],[15,0.71875775] 平均时间:0-1ms

5.dotNet 随机数产生器

写了尼玛整整一个类啊我擦,不过是源码转写进来的,也没费什么力气。代码:

(DotNetRandom 类)package com.awakening.code.random;/*** This is a random generator which translated from dotNet.* @Author:AwakeningCode* @Date: Created in 1:07 2018\\1\\16 0016*/public class DotNetRandom {    private final static int MBIG = Integer.MAX_VALUE;    private final static int MSEED = 161803398;    private int inext, inextp;    private int[] SeedArray = new int[56];    public DotNetRandom() {        this((int) System.currentTimeMillis());}    public DotNetRandom(int Seed) {        int ii;        int mj, mk;mj = MSEED - Math.abs(Seed);SeedArray[55] = mj;mk = 1;        for (int i = 1; i < 55; i++) {            /** Apparently the range [1..55] is special (Knuth) and so we\'re* wasting the 0\'th position.*/ii = (21 * i) % 55;SeedArray[ii] = mk;mk = mj - mk;            if (mk < 0)mk += MBIG;mj = SeedArray[ii];}        for (int k = 1; k < 5; k++) {            for (int i = 1; i < 56; i++) {SeedArray[i] -= SeedArray[1 + (i + 30) % 55];                if (SeedArray[i] < 0) SeedArray[i] += MBIG; } } inext = 0; inextp = 21; Seed = 1; } /** * Return a new random number [0,1) and reSeed the Seed array. * @return A double [0,1) */ protected double rand() { int retVal; int locINext = inext; int locINextp = inextp; if (++locINext >= 56)locINext = 1;        if (++locINextp >= 56)locINextp = 1;retVal = SeedArray[locINext] - SeedArray[locINextp];        if (retVal < 0)retVal += MBIG;SeedArray[locINext] = retVal;inext = locINext;inextp = locINextp;        /** Including this division at the end gives us significantly improved* random number distribution.*/return (retVal * (1.0 / MBIG));}}package com.awakening.code.random;/*** @Author:AwakeningCode* @Date: Created in 1:08 2018\\1\\16 0016*/public class TestDotNetRandom {    /*** This is a method of doNet random number generator.** @param x*            A seed for generator.* @return A float random value between [0.0,1.0)*/public static float randomDoNet(java.lang.Integer x) {        return (float) new DotNetRandom(1000 * x).rand();}    public static void main(String[] args) {        String[] Array = new String[16];        long add = 0;        for (int i = 1; i < 16 ; i++){            long startTime = System.currentTimeMillis();System.out.println(\"开始测试 randomWH() :Generate numbers(1D)\");            float result = randomDoNet(i);System.out.println(\"本次测试结果:\" + result);            long endTime = System.currentTimeMillis();            long usingTime = endTime - startTime;System.out.println(\"结束测试 randomWH() :测试结束,所用时间:\" + usingTime + \"ms\");            add += usingTime;            Array[i] = i + \",\" + result;}        long average = add / 24;System.out.println(\"本次测试平均时间:\" + average + \"ms\");System.out.print(\"[\");        for (int i = 0; i < Array.length; i++){System.out.print(\"[\" + Array[i] + \"],\");}System.out.print(\"]\");}}

[1,0.15155746],[2,0.57687163],[3,0.0021858374],[4,0.42750004],[5,0.8528142],[6,0.27812842],[7,0.7034426],[8,0.12875678],[9,0.55407095],[10,0.97938514],[11,0.40469936],[12,0.8300135],[13,0.25532773],[14,0.6806419],[15,0.10595611] 平时时间:0-1ms

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 探索各种随机函数(Java环境)