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 阅读(230) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年10月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜