package com.companyname.common.test; import java.util.ArrayList; import java.util.Random; /** * @Description 获取 10 个 1-20 之间的随机数,要求不能重复 * @Author Created by shusheng. * @Email shusheng@yiji.com * @Date 2018/12/14 */ public class RandomDemo { public static void main(String[] args) { Random random = new Random(); ArrayList<Integer> arrayList = new ArrayList<Integer>(); int x=0; while(x<10){ int y = random.nextInt(20); if(!arrayList.contains(y)){ arrayList.add(y); x++; } } for(Integer i:arrayList){ System.out.println(i); } } }