java随机数-随机生成1-16的整数

第一种方法

Random r=new Random();
int a=r.nextInt(16)+1;

解析

r.nextInt(16)会生成0-15

第二种方法
int num = (int) (Math.random() * 16 + 1);

解析

Math.random()会随机生成[0,1),大于等于0小于1

当随机生成0时,Math.random() * 16为0

当随机生成0.99时,Math.random() * 16为15.84

猜你喜欢

转载自www.cnblogs.com/ywb2018/p/10150840.html