bayard 02/04/23 06:34:01
Modified: lang/src/java/org/apache/commons/lang Strings.java
Log:
Bug submitted by: "L. Mohan Arun".
The random methods return the same value within the same millisecond due
to the methods creating a new Random object each time. I've pulled out the
Random object and made it static so it can be reused.
Revision Changes Path
1.3 +11 -4
jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java
Index: Strings.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Strings.java 15 Apr 2002 21:55:02 -0000 1.2
+++ Strings.java 23 Apr 2002 13:34:01 -0000 1.3
@@ -85,7 +85,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Greg Coladonato</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Bayard</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Ed Korthof</a>
- * @version $Id: Strings.java,v 1.2 2002/04/15 21:55:02 bayard Exp $
+ * @version $Id: Strings.java,v 1.3 2002/04/23 13:34:01 bayard Exp $
*/
public class Strings
{
@@ -96,6 +96,13 @@
public static int CHAR_BUFFER_SIZE = 4 * 1024;
/**
+ * Random object used by random method. This has to be not local
+ * to the random method so as to not return the same value in the
+ * same millisecond.
+ */
+ private static Random RANDOM = new Random();
+
+ /**
* Trims text safely, dealing with <code>null</code> references by
* converting them to <code>""</code> (the empty string).
*
@@ -1703,16 +1710,16 @@
end = Integer.MAX_VALUE;
}
}
- Random rnd = new Random();
+
StringBuffer buffer = new StringBuffer();
int gap = end - start;
while(count-- != 0) {
char ch;
if(set == null) {
- ch = (char)(rnd.nextInt(gap) + start);
+ ch = (char)(RANDOM.nextInt(gap) + start);
} else {
- ch = set[rnd.nextInt(gap) + start];
+ ch = set[RANDOM.nextInt(gap) + start];
}
if( (letters && numbers && Character.isLetterOrDigit(ch)) ||
(letters && Character.isLetter(ch)) ||
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>