Author: sebb
Date: Thu Feb  4 22:29:35 2010
New Revision: 906693

URL: http://svn.apache.org/viewvc?rev=906693&view=rev
Log:
Math.abs(long) can return a negative number
Fix nextInt() and nextLong() so all values 0 -> MAX_VALUE are equally likely

Modified:
    
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/JVMRandom.java

Modified: 
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/JVMRandom.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/JVMRandom.java?rev=906693&r1=906692&r2=906693&view=diff
==============================================================================
--- 
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/JVMRandom.java
 (original)
+++ 
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/JVMRandom.java
 Thu Feb  4 22:29:35 2010
@@ -94,7 +94,11 @@
      * @return the random int
      */
     public int nextInt() {
-        return nextInt(Integer.MAX_VALUE);
+        int value = Math.abs(SHARED_RANDOM.nextInt());
+        if (value < 0){ // Integer.MIN_VALUE
+            value = 0; // ensures 0 occurs equally often as other +ve values
+        }
+        return value;
     }
 
     /**
@@ -117,7 +121,11 @@
      * @return the random long
      */
     public long nextLong() {
-        return Math.abs(SHARED_RANDOM.nextLong());
+        long value = Math.abs(SHARED_RANDOM.nextLong());
+        if (value < 0){ // Long.MIN_VALUE
+            value = 0; // ensures 0 occurs equally often as other +ve values
+        }
+        return value;
     }
 
 


Reply via email to