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

  • 相关阅读:
    File
    多态
    方法重载
    Math
    instanceof
    强制类型转换
    泛型
    springboot热部署
    iOS bug处理
    iOS8-xcode6中添加pch全局引用文件
  • 原文地址:https://www.cnblogs.com/guhao123/p/4141405.html
Copyright © 2011-2022 走看看