From 1fd549ddbb1919a70fae4efaf0377a26a2dcd1e4 Mon Sep 17 00:00:00 2001 From: samskivert Date: Mon, 9 Mar 2009 21:09:32 +0000 Subject: [PATCH] Added getBoolean() (from Dave). git-svn-id: https://samskivert.googlecode.com/svn/trunk@2534 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/RandomUtil.java | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/java/com/samskivert/util/RandomUtil.java b/src/java/com/samskivert/util/RandomUtil.java index 8e98b934..a3715a30 100644 --- a/src/java/com/samskivert/util/RandomUtil.java +++ b/src/java/com/samskivert/util/RandomUtil.java @@ -67,7 +67,8 @@ public class RandomUtil public static int getInt (int high, int low) { 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); } @@ -113,6 +114,24 @@ public class RandomUtil 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 * element.