1.
public int getRandom() {
int number = 0;
while (true) {
number = (int) (Math.random() * 1000);//1000控制位数 1000 :三位 10000:四位 100000:五位
if (number >= 100 && number < 1000) {//100随机位数
break;
}
}
return number;
}
2.不限制随机数大小
/**
* 随机生成 num位数字字符数组
*
* @param num
* @return
*/
public static int generateRandomArray() {
int[] array = {0,1,2,3,4,5,6,7,8,9};
Random rand = new Random();
for (int i = 10; i > 1; i--) {
int index = rand.nextInt(i);
int tmp = array[index];
array[index] = array[i - 1];
array[i - 1] = tmp;
}
int result = 0;
for(int i = 0; i < 5; i++){
result = result * 10 + array[i];
}
return result;
}