Here you can find the source of generateRandomIntegers(int numbersNeeded, int bound)
public static Set<Integer> generateRandomIntegers(int numbersNeeded, int bound)
//package com.java2s; //License from project: Open Source License import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class Main { public static Set<Integer> generateRandomIntegers(int numbersNeeded, int bound) { if (numbersNeeded > bound) { throw new RuntimeException("Can not generate number more than bound"); }/*from w w w . j a v a 2 s . c om*/ Random rng = new Random(); Set<Integer> generated = new LinkedHashSet<Integer>(); while (generated.size() < numbersNeeded) { Integer next = rng.nextInt(bound); generated.add(next); } return generated; } }