//1-3的整数int n = (int)(Math.random() % 3) + 1;其中(int)(Math.random() % 3)生成一个[0,2]之间的数据,然后加1实现平移到1-3 建议阅读我写的随机数字控制的文章:http://blog.csdn.net/Mailbomb/archive/2009/03/11/3981177.aspx
public class RandomTest {
public static void main(String[] args) {
java.util.Random r=new java.util.Random(); //这个是专门产生随机数的类
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
java.util.Random r=new java.util.Random(); //这个是专门产生随机数的类
for(int i=0;i<10;i++){
System.out.println("random: "+(r.nextInt(3)+1));
}
}
}
你会生成0-2么,加1就是1-3
以上是个人意见,仅供参考