用Math.random()方法生成随机数

来源:互联网 发布:c语言计算1到n的乘积 编辑:程序博客网 时间:2024/05/19 20:59

生成的区间为[0,1);

//计算随机数公式:*(最大值-最小值+1)+最小值

//求200到18之间的随机数

//double rendom=Math.random();

//System.out.println(rendom);

for(int i=0;i<10;i++) {

int num=(int)(Math.random()*((200-18)+1)+18);//强转为int类型

System.out.println(num);