package com.companyname.common.test; import java.util.ArrayList; import java.util.Random; /** * @Description 获取 10 个 1-20 之间的随机数,要求不能重复 * @Author Created by shusheng. * @Email [email protected] * @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); } } }
原文地址:https://www.cnblogs.com/zuixinxian/p/10340856.html
时间: 2024-11-04 01:18:01