- import java.util.Comparator;
- import java.util.Random;
- import java.util.TreeSet;
- //产生十个1-20的随机数,从大到小排序,不能重复
- public class RandomDamo {
- public static void main(String[] args) {
-
- Random random = new Random();
- TreeSet<Integer> ts= new TreeSet<Integer>(new Comparator<Integer>() {
- @Override
- public int compare(Integer a1, Integer a2) {
-
- return a2-a1;
- }
- });
- int count=0;
- while(count<10) {
- long round = Math.round(Math.random());
- int i= (int)(round*20)+1;
- //int i= random.nextInt(20);
- if (!ts.contains(i)) {
- ts.add( i);
- count++;
- }
- }
-
- for (Integer integer : ts) {
- System.out.print(integer+", ");
- }
- }
- }
复制代码 |
|