import java.text.SimpleDateFormat;标签:mylist,重复,new,util,int,随机数,import,java,不会 From: https://www.cnblogs.com/hk-ing-blog/p/17271436.html
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class MyRandom {
public static void main(String[] args) {
// int radom = (int) (Math.random() * 10000);
// System.out.println(radom);
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(currentTime);
int[] intRandom = new int[200];//注意这里的数组是需要生产多少个不重复的随机数
List mylist = new ArrayList(); //生成数据集,用来保存随即生成数,并用于判断
Random rd = new Random();
while (mylist.size() < 200) {
int num = rd.nextInt(201)+1000;//将生成的随机数变成整数
if (!mylist.contains(num)) {
mylist.add(num); //往集合里面添加数据。
}
}
for (int i = 0; i < mylist.size(); i++) {
intRandom[i] = (Integer) (mylist.get(i));
}
for (int i = 0; i < mylist.size(); i++) {
System.out.println("SN"+dateString+intRandom[i]);
}
}
}