Math.random()

来源:互联网 发布:淘宝靠谱的美国代购 编辑:程序博客网 时间:2024/06/07 20:33

转载自http://blog.csdn.net/fl442165035/article/details/5832823


Math.random()随机数范围

 Math.random() //系统默认随机数范围 0.0~ 0.9之间的double

/*

*注意错误使用方法

*当需要一个int型的随机数那么就(int)Math.random()这只能得到一个数 就是 0

*/

//需要int型的 0 ~ x (x为任意大于0的整数)范围的随机数

//需要0到某个数n的范围就是Math.random()*(n+1)          

// 0 ~ 5之间就是乘以6

(int)(Math.random() * 6);   //注意:不能写成(int)Math.random()* 5 这样将永远得到0

// 0 ~ 3之间就是乘以4

(int)(Math.random() * 4);

//需要int型的 1 ~ x(x为任意大于1的整数)范围的随机数

//需要1到某个数n的范围就是Math.random()*n + 1

// 1 ~ 5之间就是乘以51

(int)(Math.random() * 5 + 1);   //注意:不能写成(int)Math.random()* 5 + 1这样将永远得到1

// 1 ~ 3之间就是乘以31

(int)(Math.random() * 3 + 1);

//如果在需要别的范围的就需要用if来筛选了

while(true){

 intnum = (int)(Math.random() * 6 + 1);  // 1 ~ 6 之间

 if(num > 2){                         //筛选出 3 ~ 6 之间的数

   System.out.println(num);

 }

}