diff --git a/src/java/org/apache/commons/lang/RandomStringUtils.java b/src/java/org/apache/commons/lang/RandomStringUtils.java index 6768a13cd..2fb5adafa 100644 --- a/src/java/org/apache/commons/lang/RandomStringUtils.java +++ b/src/java/org/apache/commons/lang/RandomStringUtils.java @@ -63,7 +63,7 @@ import java.util.Random; * @author Steven Caswell * @author Stephen Colebourne * @since 1.0 - * @version $Id: RandomStringUtils.java,v 1.7 2002/12/23 00:32:24 scolebourne Exp $ + * @version $Id: RandomStringUtils.java,v 1.8 2003/02/02 03:46:13 bayard Exp $ */ public class RandomStringUtils { @@ -191,18 +191,16 @@ public class RandomStringUtils { public static String random(int count, int start, int end, boolean letters, boolean numbers) { return random(count, start, end, letters, numbers, null); } - + /** - *

Creates a random string based on a variety of options.

+ *

Creates a random string based on a variety of options, using + * default source of randomness.

* - *

If start and end are both 0, start and end are set - * to ' ' and 'z', the ASCII printable - * characters, will be used, unless letters and numbers are both - * false, in which case, start and end are set to - * 0 and Integer.MAX_VALUE. - * - *

If set is not null, characters between start and - * end are chosen.

+ * This method has exactly the same semantics as {@link + * #random(int,int,int,boolean,boolean,char[],Random)}, but + * instead of depending on internal source of randomness ({@link + * #RANDOM}) it uses externally supplied instance of {@link + * Random} class. * * @param count length of random string to create * @param start position in set of chars to start at @@ -216,6 +214,40 @@ public class RandomStringUtils { * (end - start) + 1 characters in the set array. */ public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set) { + return random(count,start,end,letters,numbers,set,RANDOM); + } + + /** + *

Creates a random string based on a variety of options, using + * supplied source of randomness.

+ * + *

If start and end are both 0, start and end are set + * to ' ' and 'z', the ASCII printable + * characters, will be used, unless letters and numbers are both + * false, in which case, start and end are set to + * 0 and Integer.MAX_VALUE. + * + *

If set is not null, characters between start and + * end are chosen.

+ * + *

As a source of randomness is used supplied {@link Random} + * instance. This makes method behave predictively, and allows + * usage of RandomStringUtils in situations that need + * repetitive behaviour.

+ * + * @param count length of random string to create + * @param start position in set of chars to start at + * @param end position in set of chars to end before + * @param letters only allow letters? + * @param numbers only allow numbers? + * @param set set of chars to choose randoms from. If null, + * then it will use the set of all chars. + * @param random source of randomness. + * @return the random string + * @throws ArrayIndexOutOfBoundsException if there are not + * (end - start) + 1 characters in the set array. + */ + public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] set, Random random) { if( (start == 0) && (end == 0) ) { end = (int)'z'; start = (int)' '; @@ -231,9 +263,9 @@ public class RandomStringUtils { while(count-- != 0) { char ch; if(set == null) { - ch = (char)(RANDOM.nextInt(gap) + start); + ch = (char)(random.nextInt(gap) + start); } else { - ch = set[RANDOM.nextInt(gap) + start]; + ch = set[random.nextInt(gap) + start]; } if( (letters && numbers && Character.isLetterOrDigit(ch)) || (letters && Character.isLetter(ch)) || diff --git a/src/test/org/apache/commons/lang/RandomStringUtilsTest.java b/src/test/org/apache/commons/lang/RandomStringUtilsTest.java index 14c7753ea..c3b479479 100644 --- a/src/test/org/apache/commons/lang/RandomStringUtilsTest.java +++ b/src/test/org/apache/commons/lang/RandomStringUtilsTest.java @@ -54,6 +54,8 @@ package org.apache.commons.lang; * . */ +import java.util.Random; + import junit.framework.*; import junit.textui.TestRunner; /** @@ -61,7 +63,7 @@ import junit.textui.TestRunner; * * @author Steven Caswell * @author Ringo De Smet - * @version $Id: RandomStringUtilsTest.java,v 1.2 2002/10/08 19:01:39 sullis Exp $ + * @version $Id: RandomStringUtilsTest.java,v 1.3 2003/02/02 03:46:13 bayard Exp $ */ public class RandomStringUtilsTest extends junit.framework.TestCase { /** @@ -148,6 +150,11 @@ public class RandomStringUtilsTest extends junit.framework.TestCase { } r2 = RandomStringUtils.random(50, set); assertTrue("!r1.equals(r2)", !r1.equals(r2)); + + long seed = System.currentTimeMillis(); + r1 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed)); + r2 = RandomStringUtils.random(50,0,0,true,true,null,new Random(seed)); + assertEquals("r1.equals(r2)", r1, r2); } public static void main(String args[]) {