From a4d53dcbb87e9ec7dd486f6b923cb026bf6fabb8 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Wed, 31 Mar 2010 21:06:04 +0000 Subject: [PATCH] Added getRange(), in int and float versions, and deprecated getInt(high, low), because the parameter ordering and exclusive-of-low makes everybody cry. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2765 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/RandomUtil.java | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/java/com/samskivert/util/RandomUtil.java b/src/java/com/samskivert/util/RandomUtil.java index 608f7ee7..1fbe9b8b 100644 --- a/src/java/com/samskivert/util/RandomUtil.java +++ b/src/java/com/samskivert/util/RandomUtil.java @@ -63,14 +63,31 @@ public class RandomUtil /** * Returns a pseudorandom, uniformly distributed int value between * high and low, exclusive of each. + * + * @deprecated use getRange(int, int). */ + @Deprecated public static int getInt (int high, int low) { - if (high - low - 1 <= 0) { - throw new IllegalArgumentException( - "Invalid range [high=" + high + ", low=" + low + "]"); - } - return low + 1 + rand.nextInt(high - low - 1); + return getRange(low + 1, high); + } + + /** + * Returns a pseudorandom, uniformly distributed int value between + * low (inclusive) and high (exclusive). + */ + public static int getRange (int low, int high) + { + return low + rand.nextInt(high - low); + } + + /** + * Returns a pseudorandom, uniformly distributed float value between + * low (inclusive) and high (exclusive). + */ + public static float getRange (float low, float high) + { + return low + (rand.nextFloat() * (high - low)); } /**