java 语言实现的随机数生成算法

来源:互联网 发布:网络棋牌广告语 编辑:程序博客网 时间:2024/04/29 05:36
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;}}


原创粉丝点击