[Java] 纯文本查看 复制代码
import java.util.Random;
/**
* @program: Test
* @Date: 2018-07-23 10:47
* @Author: lzq
* @Description:生成一个随机四位数,每位数字不能重复
*/
public class GenerateNum {
public static void main(String[] args) {
Random random = new Random();
int arr[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
String str = "";
int temp = 0;
while (str.length() != 4) {
temp = random.nextInt(10);//随机获取0~9的数字
if (arr[temp] == 0) {
str += temp;
arr[temp] = 1;
}
}
System.out.println(str);
}
}
[Java] 纯文本查看 复制代码
import java.util.*;
/**
* @program: Test
* @Date: 2018-07-23 9:33
* @Author: lzq
* @Description:
*/
public class GetNum {
private static final List<Integer> list = new ArrayList(10000);
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
int num = (int) ( Math.random() * (10000 - 0 + 1));
list.add(num);
}
list.sort(Integer::compareTo);
System.out.println(list.indexOf(9999));
}
}