获取1-20之间不重复的10个随机数

package set;

import java.util.HashSet;
import java.util.Random;

//获取1-20之间的不重复的10个随机数

public class RandomNum {
	public static void main(String[] args) {
		//1.random生成随机数
		Random r = new Random();
		//2.hashSet存储结果
		HashSet<Integer> hs = new HashSet<Integer>();
		//3.size>10时,停止放数
		while(hs.size()<10){
			//4.生成随机数
			int res = r.nextInt(20)+1;
			//5.添加到集合
			hs.add(res);
		}
		System.out.println(hs);
	}
}

猜你喜欢

转载自blog.csdn.net/qzcrystal/article/details/82814408