我想用JAVA编一个能产生服从正态分布的随机数的程序,不知该如何编写,请大虾不吝赐教!!教我如何求积分也行.

解决方案 »

  1.   

    根据独立同分布的中心极限定理,有:
    这里,其实只要取n=12(这里,亦即生成12个0-1上的随机数序列)就会有比较好的效果。 经验证,用该种方法生成生的随机数序列同样能比较好的符合正态分布特性。由于生成的都是标准正态分布,所以,当需要生成N(a,b)的正态分布随机量时,根据正态分布的线性变换特性,只要用x=b*x0+a即可。(其中,x0表示生成的符合N(0,1)分布的正态随机变量。)自己编的代码如下:public double Norm_rand(double miu, double sigma2){
      double N = 12;
      double x=0,temp=N;
      do{
       x=0;
       for(int i=0;i<N;i++)
        x=x+(Math.random());
       x=(x-temp/2)/(Math.sqrt(temp/12));
       x=miu+x*Math.sqrt(sigma2);
       }while(x<=0);          //在此我把小于0的数排除掉了
       return x;
     }
      

  2.   

    java.util.Random 类下有个 nextGaussian() 方法,就是用于产生服从正态分布数的,
    没有必要自己再实现一个了。
      

  3.   

    感謝!
    nextGaussian不可重寫,所以用起來不理想。一樓分析得很到位,謝謝
      

  4.   

    java.util.Random r = new java.util.Random();
    double ans = r.nextGaussian()*Math.sqrt(b)+a;r.nextGaussian()服从N(0,1)
    ans服从N(a,b)又何必重写~
    非要重写,请看javadocThe method nextGaussian is implemented by class Random as if by a threadsafe version of the following:
     private double nextNextGaussian;
     private boolean haveNextNextGaussian = false; public double nextGaussian() {
       if (haveNextNextGaussian) {
         haveNextNextGaussian = false;
         return nextNextGaussian;
       } else {
         double v1, v2, s;
         do {
           v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
           v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
           s = v1 * v1 + v2 * v2;
         } while (s >= 1 || s == 0);
         double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
         nextNextGaussian = v2 * multiplier;
         haveNextNextGaussian = true;
         return v1 * multiplier;
       }
     }
    This uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P. Note that it generates two independent values at the cost of only one call to StrictMath.log and one call to StrictMath.sqrt.
      

  5.   

    如何产生100个服从zipf分布的随机数???
      

  6.   

    可以重写,怎么不能重写了,又没有被什么为public final double nextGaussian()
      

  7.   

    java中怎样实现java中100万个数据正态分布