Java Random Number distinctInts(int from, int to, int number)

Here you can find the source of distinctInts(int from, int to, int number)

Description

Returns from the range of [from, to] (inclusive).

License

Open Source License

Parameter

Parameter Description
from a parameter
to a parameter
number a parameter

Declaration

public static int[] distinctInts(int from, int to, int number) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Main {
    private static final Random rand = new Random();

    /**//  w w w . j  a v  a2  s . com
     * Returns from the range of [from, to] (inclusive). "Number" amount of distinct integers.
     * @param from
     * @param to
     * @param number
     * @return
     */
    public static int[] distinctInts(int from, int to, int number) {
        if (from >= to)
            throw new IllegalArgumentException("From cannot be larget than to!");

        List<Integer> lst = new ArrayList<Integer>();
        for (int i = from; i <= to; i++)
            lst.add(i);

        Collections.shuffle(lst, rand);
        int[] ret = new int[number];
        for (int i = 0; i < number; i++)
            ret[i] = lst.get(i);

        return ret;
    }
}

Related

  1. createParameters(int numberOfParameters)
  2. createRandomFilename()
  3. createRandomHexNumber(int length)
  4. createRandomNumber()
  5. createRandomNumber(int index)
  6. generateRandomNum(int i)
  7. generateRandomNum(int passNumber)
  8. generateRandomNumb(int randomSize, int range)
  9. generateRandomNumber()