Math.random随机,上码:
Math.random();//生成出0-1之间的随机小数
Math.round();//四舍五入方式取得最接近的整数
Math.ceil(); //是向上取得一个最接近的整数
Math.floor(); //是向下取得一个最接近的整数
//随机小数后 取整数,产生 0 或者 1。
Math.round(Math.random());
//乘以10再取整数
Math.round(Math.random()*10);
//生成一个1到10之间的随机整数(ceil向上取整数,所以不会出现0)
Math.ceil(Math.random()*10);
生成一个四位数的随机数,且四个数不重复:
public int[] randoms(){
int[] randoms = new int[4];
Vector<Integer> vector = new Vector<Integer>();
for (int i=0; i<4; i++){
int random = (int) Math.floor(Math.random()*10);
while(vector.contains(random)){
random = (int) Math.floor(Math.random()*10);
}
vector.add(random);
randoms[i]=random;
}
return randoms;
}