qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          java 語言實現的隨機數生成算法

          package MyMath;
          import java.util.Random;
          //生成隨機數  調用的是系統的方法
          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 類庫的實現方法
            下面自己寫隨機,,了解一下種子數,,其實對同一個種子生成的隨機數是相同的,,但是種子數是不對更新的
          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);  隨機數 p=r[i/base;
          //這個隨機數 確實是隨機的  但是缺陷就是它并不符合 正態分布  種子的選取會影響后來的分布的
          }
          }


           引用一些公式就實現了符合正態分布的
          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));
          }
          }
          //符合正態分布的隨機算法
          /*
          *
          *
          */
          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);  隨機數 p=r[i/base;
          //這個隨機數 確實是隨機的  但是缺陷就是它并不符合 正態分布  種子的選取會影響后來的分布的
          }
          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 閱讀(243) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2013年10月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南雄市| 景泰县| 济宁市| 阳曲县| 福建省| 德安县| 苏州市| 武安市| 九寨沟县| 新野县| 佛学| 合作市| 西乌| 大庆市| 禹城市| 呼伦贝尔市| 永宁县| 利辛县| 安福县| 长沙县| 枣庄市| 双峰县| 合肥市| 子长县| 香港| 怀集县| 阳朔县| 西林县| 沿河| 高陵县| 周口市| 巴彦淖尔市| 五台县| 林芝县| 高邑县| 娄底市| 阳原县| 永靖县| 英吉沙县| 合水县| 永丰县|