Java Random Int generateRandomIntegers(int numbersNeeded, int bound)

Here you can find the source of generateRandomIntegers(int numbersNeeded, int bound)

Description

generate Random Integers

License

Open Source License

Declaration

public static Set<Integer> generateRandomIntegers(int numbersNeeded, int bound) 

Method Source Code


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

Related

  1. generateRandomInt(final int max)
  2. generateRandomInt(int min, int max)
  3. generateRandomInteger(int aStart, int aEnd, Random aRandom)
  4. generateRandomInteger(int start, int end)
  5. generateRandomIntegerArray(int size, int min, int max)
  6. generateRandomIntInRange(int min, int max)
  7. generateRandomIntObj(int range, int noOfElements)
  8. generateRandomInts(int range, int noOfElements)
  9. generateRandomIntUpto(int max)