Here you can find the source of randomNums(int min, int max, int count)
public static List<Integer> randomNums(int min, int max, int count)
//package com.java2s; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; public class Main { public static List<Integer> randomNums(int min, int max, int count) { if (max - min + 1 > count) { count = max - min + 1;//from w w w .ja va 2s . c o m } Set<Integer> nums = new LinkedHashSet<Integer>(); while (nums.size() < count) { nums.add(randomNum(min, max)); } List<Integer> list = new ArrayList<Integer>(nums); return list; } public static int randomNum(int min, int max) { if (min < 0) { int i = nextInt(max + 1 + Math.abs(min)); return min + i; } else { int i = nextInt(max + 1 - min); return min + i; } } public static int nextInt(int val) { return nextInt(val, true); } public static int nextInt(int val, boolean isAbs) { int r = (int) (ThreadLocalRandom.current().nextDouble() * val); if (isAbs) { return Math.abs(r); } return r; } }