commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [2/4] commons-rng git commit: RNG-20
Date Mon, 10 Oct 2016 00:47:09 GMT
RNG-20

Modified "fillState" method: new implementation is based on code used in "AbstractWell" class.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/a0661674
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/a0661674
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/a0661674

Branch: refs/heads/master
Commit: a06616742e03e79c80e757adb5baaf2349289cef
Parents: 120e2c5
Author: Gilles <erans@apache.org>
Authored: Sun Oct 9 15:25:43 2016 +0200
Committer: Gilles <erans@apache.org>
Committed: Sun Oct 9 15:25:43 2016 +0200

----------------------------------------------------------------------
 .../commons/rng/internal/util/SeedFactory.java  | 69 ++++++++++++++++++--
 1 file changed, 64 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/a0661674/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java b/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
index 047e9f0..11fcfb1 100644
--- a/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
+++ b/src/main/java/org/apache/commons/rng/internal/util/SeedFactory.java
@@ -123,13 +123,39 @@ public class SeedFactory {
 
         if (seedSize < stateSize) {
             for (int i = seedSize; i < stateSize; i++) {
-                state[i] = 26021969 * i;
-            }
-            for (int i = stateSize - 1; i > seedSize; i--) {
-                state[i] ^= state[stateSize - i - 1];
+                state[i] = (int) (scrambleWell(state[i - seed.length], i) & 0xffffffffL);
             }
+        }
+    }
+
+    /**
+     * Simple filling procedure.
+     * It will
+     * <ol>
+     *  <li>
+     *   fill the beginning of {@code state} by copying
+     *   {@code min(seed.length, state.length)} elements from
+     *   {@code seed},
+     *  </li>
+     *  <li>
+     *   set all remaining elements of {@code state} with non-zero
+     *   values (even if {@code seed.length < state.length}).
+     *  </li>
+     * </ol>
+     *
+     * @param state State. Must be allocated.
+     * @param seed Seed. Cannot be null.
+     */
+    public static void fillState(long[] state,
+                                 long[] seed) {
+        final int stateSize = state.length;
+        final int seedSize = seed.length;
+        System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize));
 
-            state[seedSize] = 0x80000000; // Ensuring non-zero initial array.
+        if (seedSize < stateSize) {
+            for (int i = seedSize; i < stateSize; i++) {
+                state[i] = scrambleWell(state[i - seed.length], i);
+            }
         }
     }
 
@@ -295,4 +321,37 @@ public class SeedFactory {
             return source.next() ^ number;
         }
     }
+
+    /**
+     * Transformation used to scramble the initial state of
+     * a generator.
+     *
+     * @param n Seed element.
+     * @param mult Multiplier.
+     * @param shift Shift.
+     * @param add Offset.
+     * @return the transformed seed element.
+     */
+    private static long scramble(long n,
+                                 long mult,
+                                 int shift,
+                                 int add) {
+        // Code inspired from "AbstractWell" class.
+        return mult * (n ^ (n >> shift)) + add;
+    }
+
+    /**
+     * Transformation used to scramble the initial state of
+     * a generator.
+     *
+     * @param n Seed element.
+     * @param add Offset.
+     * @return the transformed seed element.
+     * @see #scramble(long,long,int,int)
+     */
+    private static long scrambleWell(long n,
+                                     int add) {
+        // Code inspired from "AbstractWell" class.
+        return scramble(n, 1812433253L, 30, add);
+    }
 }


Mime
View raw message