Math.random()随机数

可以通过 Math.random()获取一个[0,1)的double型随机数;

int s=(int) (Math.random()*50);//生成一个0~50的随机整数
int k=(int) (100-(Math.random()*50));//生成一个50~100的随机整数
System.out.println(k);
System.out.println(s);

注意:千万不要写成 “ (int)Math.random()*100 ” 括号一定要括上否则结果为0.

猜你喜欢

转载自www.cnblogs.com/cuizhufeng/p/11797584.html