Added getBoolean() (from Dave).

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2534 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2009-03-09 21:09:32 +00:00
parent 6013c50a50
commit 1fd549ddbb
+20 -1
View File
@@ -67,7 +67,8 @@ public class RandomUtil
public static int getInt (int high, int low) public static int getInt (int high, int low)
{ {
if (high - low - 1 <= 0) { if (high - low - 1 <= 0) {
throw new IllegalArgumentException("Invalid range [high=" + high + ", low=" + low + "]"); throw new IllegalArgumentException(
"Invalid range [high=" + high + ", low=" + low + "]");
} }
return low + 1 + rand.nextInt(high - low - 1); return low + 1 + rand.nextInt(high - low - 1);
} }
@@ -113,6 +114,24 @@ public class RandomUtil
return getInt(n, r) == 0; return getInt(n, r) == 0;
} }
/**
* Returns a pseudorandom, uniformly distributed boolean.
*/
public static boolean getBoolean ()
{
return getBoolean(rand);
}
/**
* Returns a pseudorandom, uniformly distributed boolean.
*
* @param r the random number generator to use
*/
public static boolean getBoolean (Random r)
{
return r.nextBoolean();
}
/** /**
* Pick a random index from the array, weighted by the value of the corresponding array * Pick a random index from the array, weighted by the value of the corresponding array
* element. * element.