From 8ac0e012ce1db71706b838f7be9f686a612e1a60 Mon Sep 17 00:00:00 2001 From: samskivert Date: Wed, 22 Oct 2008 00:55:40 +0000 Subject: [PATCH] New convenience functions from Dave. What are the chances? git-svn-id: https://samskivert.googlecode.com/svn/trunk@2456 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/RandomUtil.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/java/com/samskivert/util/RandomUtil.java b/src/java/com/samskivert/util/RandomUtil.java index f755c464..8e98b934 100644 --- a/src/java/com/samskivert/util/RandomUtil.java +++ b/src/java/com/samskivert/util/RandomUtil.java @@ -57,7 +57,7 @@ public class RandomUtil */ public static int getInt (int high, Random r) { - return r.nextInt(high); + return r.nextInt(high); } /** @@ -95,6 +95,24 @@ public class RandomUtil return r.nextFloat() * high; } + /** + * Returns true approximately one in n times. + */ + public static boolean getChance (int n) + { + return getChance(n, rand); + } + + /** + * Returns true approximately one in n times. + * + * @param r the random number generator to use + */ + public static boolean getChance (int n, Random r) + { + return getInt(n, r) == 0; + } + /** * Pick a random index from the array, weighted by the value of the corresponding array * element.