Support picking from arrays.

This commit is contained in:
David Hoover
2011-08-23 10:52:54 -07:00
parent ad377342dd
commit 7bb7ee2ad8
@@ -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