Java ThreadLocalRandom getRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize, Integer numNumbers)

Here you can find the source of getRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize, Integer numNumbers)

Description

get Random Numbers

License

Apache License

Declaration

public static List<Integer> getRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize,
            Integer numNumbers) 

Method Source Code


//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);
    }
}

Related

  1. getRandomInt(int limit)
  2. getRandomInt(int n)
  3. getRandomInt(int r)
  4. getRandomInteger()
  5. getRandomIntegerElement(List list)
  6. getRandomPassword()
  7. getRandomStr(int len)
  8. getRandomString(int length)
  9. getRandomTarget(List targets)