Author: sebb
Date: Thu Feb  4 01:31:22 2010
New Revision: 906318

URL: http://svn.apache.org/viewvc?rev=906318&view=rev
Log:
LANG-472 - RandomUtils.nextLong() get all even number

Modified:
    
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/JVMRandom.java
    
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.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=906318&r1=906317&r2=906318&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 01:31:22 2010
@@ -41,6 +41,8 @@
      */
     private static final long serialVersionUID = 1L;
 
+    private static final Random SHARED_RANDOM = new Random();
+
     /**
      * Ensures that only the constructor can call reseed.
      */
@@ -110,7 +112,7 @@
             );
         }
         // TODO: check this cannot return 'n'
-        return (int)(Math.random() * n);
+        return (int)(SHARED_RANDOM.nextDouble() * n);
     }
     /**
      * <p>Returns the next pseudorandom, uniformly distributed long value
@@ -118,8 +120,7 @@
      * @return the random long
      */
     public long nextLong() {
-        // possible loss of precision?
-        return nextLong(Long.MAX_VALUE);
+        return Math.abs(SHARED_RANDOM.nextLong());
     }
 
 
@@ -139,7 +140,7 @@
             );
         }
         // TODO: check this cannot return 'n'
-        return (long)(Math.random() * n);
+        return (long)(SHARED_RANDOM.nextDouble() * n);
      }
 
     /**
@@ -149,7 +150,7 @@
      * @return the random boolean
      */
     public boolean nextBoolean() {
-        return Math.random() > 0.5;
+        return SHARED_RANDOM.nextDouble() > 0.5;
     }
     /**
      * <p>Returns the next pseudorandom, uniformly distributed float value
@@ -159,7 +160,7 @@
      * @return the random float
      */
     public float nextFloat() {
-        return (float)Math.random();
+        return (float)SHARED_RANDOM.nextDouble();
     }
     /**
      * <p>Synonymous to the Math.random() call.</p>
@@ -167,7 +168,7 @@
      * @return the random double
      */
     public double nextDouble() {
-        return Math.random();
+        return SHARED_RANDOM.nextDouble();
     }
     
 }

Modified: 
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java?rev=906318&r1=906317&r2=906318&view=diff
==============================================================================
--- 
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java
 (original)
+++ 
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/RandomUtilsTest.java
 Thu Feb  4 01:31:22 2010
@@ -118,8 +118,12 @@
      * @param rnd Random to use if not null
      */
     private void tstNextLong(Random rnd) {
+        // Distribution
         int[] expected = new int[] {500,500};
         int[] observed = new int[] {0,0};
+        // Even/Odd
+        int[] expected2 = new int[] {500,500};
+        int[] observed2 = new int[] {0,0};
         long result = 0;
         long midPoint = Long.MAX_VALUE/2;
         for (int i = 0; i < 1000; i ++) {
@@ -133,6 +137,11 @@
             } else {
                 observed[1]++;
             }
+            if (result % 2 == 0) {
+               observed2[0]++;
+            } else {
+               observed2[1]++;
+            }
         }
         /* Use ChiSquare dist with df = 2-1 = 1, alpha = .001
          * Change to 6.64 for alpha = .01  
@@ -140,6 +149,9 @@
         assertTrue(
             "chi-square test -- will fail about 1 in 1000 times",
             chiSquare(expected,observed) < 10.83); 
+        assertTrue(
+                "chi-square test -- will fail about 1 in 1000 times",
+                chiSquare(expected2,observed2) < 10.83); 
     }
         
     


Reply via email to