Support picking from arrays.
This commit is contained in:
@@ -176,6 +176,22 @@ public class Randoms
|
||||
return pickPluck(iterable, ifEmpty, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick a random element from the specified array, or return <code>ifEmpty</code>
|
||||
* if it is empty.
|
||||
*
|
||||
* @throws NullPointerException if the array is null.
|
||||
*/
|
||||
public <T> T pick (T[] array, T ifEmpty)
|
||||
{
|
||||
int size = array.length;
|
||||
if (size == 0) {
|
||||
return ifEmpty;
|
||||
}
|
||||
|
||||
return array[_r.nextInt(size)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick a random <em>key</em> from the specified mapping of weight values, or return
|
||||
* <code>ifEmpty</code> if no mapping has a weight greater than <code>0</code>. Each
|
||||
|
||||
Reference in New Issue
Block a user