随机数算法

伪随机数:

数学公式:r[i]=(v*r[i-1] + u) mod base    

 p=r[i]/base

代码实现

package mytest;

public class MyRandom {
    /**
     * r[i]=(v*r[i-1] + u) mod base
     * p=r[i]/base
     * @param r
     */
    static double random(double[] r){
        double temp1,temp2,temp3,base,u,v,p;
        base=256.0;
        u=17;
        v=139;
        temp1=v*r[0]+u;
        temp2=(int)(temp1/base);
        temp3=temp1-temp2*base;
        r[0]=temp3;
        p=temp3/base;
        return p;
        
    }
    public static void main(String[] args) {
        double[] r={5.0};
        for (int i = 0; i < 10; i++) {
            System.out.println(random(r));
        }
        
    }

}

r[0]作为随机数的种子。每次更新。

原文地址:https://www.cnblogs.com/guhao123/p/4141405.html