Here you can find the source of genRandomNumList(int num, int count)
public static List<Integer> genRandomNumList(int num, int count)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; public class Main { public static List<Integer> genRandomNumList(int num, int count) { if (num < count) { return Collections.EMPTY_LIST; }/* w w w . ja v a 2s . co m*/ List<Integer> result = new ArrayList<Integer>(); Map<Integer, String> containMap = new HashMap<Integer, String>(); Integer temp = null; Random random = new Random(); for (int i = 0; i < count;) { temp = random.nextInt(num); if (!containMap.containsKey(temp)) { containMap.put(temp, null); result.add(temp); i++; } } return result; } }