You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/10/10 00:47:09 UTC

[2/4] commons-rng git commit: RNG-20

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 <er...@apache.org>
Authored: Sun Oct 9 15:25:43 2016 +0200
Committer: Gilles <er...@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);
+    }
 }


Re: [2/4] commons-rng git commit: RNG-20

Posted by Emmanuel Bourg <eb...@apache.org>.
Le 10/10/2016  02:47, erans@apache.org a crit :
> RNG-20
> 
> Modified "fillState" method: new implementation is based on code used in "AbstractWell" class.

If I may suggest, I think it would be a good idea to put more
information on the first line of the commit message. The first line is
often the only visible part of the commit message in various tools
displaying the commit history (for example TortoiseGit, IntilliJ or
GitHub), so having more information there improves the history browsing.

Emmanuel Bourg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org