qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          java 語言實(shí)現(xiàn)的隨機(jī)數(shù)生成算法

          package MyMath;
          import java.util.Random;
          //生成隨機(jī)數(shù)  調(diào)用的是系統(tǒng)的方法
          public class random {
          public static void main(String args[])
          {
          Random random=new Random(5);
          for(int i=0;i<10;i++)
          {
          System.out.println(random.nextInt());
          }
          }
          }
            引用java 類庫的實(shí)現(xiàn)方法
            下面自己寫隨機(jī),,了解一下種子數(shù),,其實(shí)對同一個種子生成的隨機(jī)數(shù)是相同的,,但是種子數(shù)是不對更新的
          package MyMath;
          public class random1 {
          public static void main(String args[])
          {
          double []r=new double[2];
          r[0]=5.0;
          for(int i=0;i<10;i++)
          {
          System.out.println(rand1(r));
          }
          }
          public static double rand1(double []r)
          {
          double temp1,temp2,temp3,p,base;
          base=256.0;
          int  a=17,b=139;
          temp1=r[0]*17+139;
          temp2=(int)(temp1/256);
          temp3=temp1-temp2*base;
          r[0]=temp3;
          p=temp3/256;
          return p;
          //基本思想 就是   遞推法  r[i]=mod(a*r[i-1],base);  隨機(jī)數(shù) p=r[i/base;
          //這個隨機(jī)數(shù) 確實(shí)是隨機(jī)的  但是缺陷就是它并不符合 正態(tài)分布  種子的選取會影響后來的分布的
          }
          }


           引用一些公式就實(shí)現(xiàn)了符合正態(tài)分布的
          public class random2 {
          public static void main(String args[])
          {
          double []r=new double[2];
          r[0]=5.0;
          for(int i=0;i<10;i++)
          {
          System.out.println(randZT(2.0,3.5,r));
          }
          }
          //符合正態(tài)分布的隨機(jī)算法
          /*
          *
          *
          */
          public static double rand1(double []r)
          {
          double temp1,temp2,temp3,p,base;
          base=256.0;
          int  a=17,b=139;
          temp1=r[0]*17+139;
          temp2=(int)(temp1/256);
          temp3=temp1-temp2*base;
          r[0]=temp3;
          p=temp3/256;
          return p;
          //基本思想 就是   遞推法  r[i]=mod(a*r[i-1],base);  隨機(jī)數(shù) p=r[i/base;
          //這個隨機(jī)數(shù) 確實(shí)是隨機(jī)的  但是缺陷就是它并不符合 正態(tài)分布  種子的選取會影響后來的分布的
          }
          public static double randZT(double u,double t,double []r)
          {
          int i;
          double total=0.0;
          double result;
          for(i=0;i<12;i++)
          {
          total+=rand1(r);
          }
          result=u+t*(total-6.0);
          return result;
          }
          }

          posted on 2013-10-16 10:51 順其自然EVO 閱讀(241) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2013年10月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 石柱| 随州市| 南充市| 于都县| 潼南县| 莱阳市| 博乐市| 洪湖市| 颍上县| 铅山县| 米脂县| 庆阳市| 河曲县| 津南区| 土默特右旗| 华池县| 河池市| 南部县| 陆河县| 金寨县| 汾阳市| 嵩明县| 夹江县| 汝城县| 德兴市| 凉山| 长沙县| 北安市| 西乡县| 龙川县| 永和县| 合山市| 五大连池市| 富平县| 福安市| 涪陵区| 大化| 盘锦市| 永修县| 东山县| 广州市|