public class RandomUtil extends Object
Constructor and Description |
---|
RandomUtil() |
RandomUtil(Random random) |
Modifier and Type | Method and Description |
---|---|
<T> T |
choose(List<? extends T> list) |
static <T> T |
choose(List<? extends T> list,
Random rand) |
<T> T |
choose(T[] array) |
static <T> T |
choose(T[] array,
Random rand) |
int[] |
distinctIntegers(int num,
int max) |
int[] |
distinctIntegers(int num,
int min,
int max)
Sample a specified number of distinct integers from a specified range.
|
static int[] |
distinctIntegers(int num,
int min,
int max,
Random rand)
Sample a specified number of distinct integers from a specified range.
|
static int[] |
distinctIntegers(int num,
int max,
Random rand) |
Random |
getRandom() |
<T> List<T> |
sample(List<? extends T> list,
int num) |
static <T> List<T> |
sample(List<? extends T> list,
int num,
Random rand) |
<T> List<T> |
sampleUnique(List<? extends T> list,
int num)
Sample a specified number of elements from specified list.
|
static <T> List<T> |
sampleUnique(List<? extends T> list,
int num,
Random rand)
Sample a specified number of elements from specified list.
|
public RandomUtil()
public RandomUtil(Random random)
public <T> T choose(T[] array)
public static <T> T choose(T[] array, Random rand)
public <T> T choose(List<? extends T> list)
public Random getRandom()
public int[] distinctIntegers(int num, int min, int max)
The implementation is based on Floyd's Algorithm F2. Note that this algorithm ensures equal probability of each integer within in the specified range to appear in the returned array but no equal probability of their order.
num
- number of integers to samplemin
- lower bound (inclusive) of sampled valuesmax
- upper bound (exclusive) of samples valuespublic static int[] distinctIntegers(int num, int min, int max, Random rand)
The implementation is based on Floyd's Algorithm F2. Note that this algorithm ensures equal probability of each integer within in the specified range to appear in the returned array but no equal probability of their order.
num
- number of integers to samplemin
- lower bound (inclusive) of sampled valuesmax
- upper bound (exclusive) of samples valuesrand
- the random instance for generating numberspublic int[] distinctIntegers(int num, int max)
public static int[] distinctIntegers(int num, int max, Random rand)
public <T> List<T> sampleUnique(List<? extends T> list, int num)
The implementation is based on Floyd's Algorithm F2. Note that this algorithm ensures equal probability of each element within in the specified list to appear in the returned list but no equal probability of their order.
list
- the list to sample elements fromnum
- number of integers to samplepublic static <T> List<T> sampleUnique(List<? extends T> list, int num, Random rand)
The implementation is based on Floyd's Algorithm F2. Note that this algorithm ensures equal probability of each element within in the specified list to appear in the returned list but no equal probability of their order.
list
- the list to sample elements fromnum
- number of integers to samplerand
- the random instance for generating numbersCopyright © 2020. All rights reserved.