java-随机数

Math.random() 返回一个0到1之间(包括0,不包括1)的随机浮点数;

用n乘以这个随机浮点数,可以得到0到n-1之间的随机数;

 

double a =  Math.random(); // 0.4921534576132002
int b = (int) (Math.random() * 10);
System.out.println(b);// 5

 

posted @ 2015-06-24 14:27  MyCloud  阅读(122)  评论(0编辑  收藏  举报