zoukankan      html  css  js  c++  java
  • 随机数算法

    伪随机数:

    数学公式: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]作为随机数的种子。每次更新。

  • 相关阅读:
    POJ 1045
    POJ 1051
    POJ 1047
    POJ 1050
    POJ 1046
    POJ 1036
    POJ 1035
    POJ 1032
    【洛谷P1412】经营与开发
    【洛谷P3377】【模板】左偏树(可并堆)
  • 原文地址:https://www.cnblogs.com/guhao123/p/4141405.html
Copyright © 2011-2022 走看看