Here you can find the source of getRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize, Integer numNumbers)
public static List<Integer> getRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize, Integer numNumbers)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; public class Main { public static List<Integer> getRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize, Integer numNumbers) { Set<Integer> integers = new HashSet<>(); int maxNumbers = (endNumber - startNumber) / pageSize; if (numNumbers > maxNumbers) { numNumbers = maxNumbers;//from ww w . jav a 2 s .co m } while (integers.size() < numNumbers) { int i = ThreadLocalRandom.current().nextInt(startNumber, endNumber + 1); i = (i / pageSize) * pageSize; integers.add(i); } return new ArrayList<>(integers); } }