Random random = new Random(); for(int i=0;i<100000;i++){ double dou = random.nextDouble(); //这个0.9999由你根据需要来控制其出现的几率。 if(dou >= 0.9999){ dou = 1.0; System.out.println(dou); } }
import java.util.*; class Random0to1 { public static void main(String[] args) { Random r = new Random(); double d =0.1; for(int i=0;i<100000;i++) { d = r.nextDouble(); if(d>0.9999) System.out.println(1); else System.out.println(new Random().nextDouble()); } } }
random.nextInt只会生成整数型,然而我需要double的,也就是0-1之间的所有小数,所以这个方法应该是不行的。。
Random random = new Random();
for(int i=0;i<100000;i++){
double dou = random.nextDouble();
//这个0.9999由你根据需要来控制其出现的几率。
if(dou >= 0.9999){
dou = 1.0;
System.out.println(dou);
}
}
class Random0to1
{
public static void main(String[] args)
{
Random r = new Random();
double d =0.1;
for(int i=0;i<100000;i++)
{
d = r.nextDouble();
if(d>0.9999)
System.out.println(1);
else
System.out.println(new Random().nextDouble());
}
}
}