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.
(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.
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;
*