public class NumUtil {
/**
* 随机数生成器
* @param digit 位数
* @return
*/
public static String randomGenerator(int digit){
int[] nums = {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 = nums[index];
nums[index] = nums[i - 1];
nums[i - 1] = tmp;
}
int result = 0;
for(int i = 0; i < digit; i++){
result = result * 10 + nums[i];
}
String digitString = Integer.toString(result);
if (digitString.length() == digit-1) {
digitString = "0" + digitString;
}
return digitString;
}
}
Java生成随机数
最新推荐文章于 2024-04-28 22:59:35 发布