JAVA编程如何产生1到33之间的随机数

如题所述

public class getRandom {
public int result;
public void run(){
result = (int)(33*Math.random()+1);//Math函数库重的random()方法可以产生0至1的随机数
System.out.println("随机产生1——32的随机数是:"+result);
}
public getRandom(){//方法调用
run();
}
public static void main(String []args){
new getRandom();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-03
(int)(Math.random()*33+1)追问

请问如何使用呢

如果我想随机产生五个随机数,该怎么办呢

第2个回答  2014-03-03
int[] arr = new int[5];
for (int i=0; i < 5; i++)
{
arr[i] = (int)(Math.random()*33 + 1);
}
第3个回答  2014-03-03
new Random().nextInt(33)+1
第4个回答  2018-04-12
qsadfjgkfaghj,asgsfdjgjdhgashhdfcvzzxvx