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/09/10 12:37:30 UTC

[3/3] commons-rng git commit: Avoid code duplication.

Avoid code duplication.


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

Branch: refs/heads/master
Commit: 2471de8168ec158749fd7e3febfde146405f6559
Parents: fe6ec47
Author: Gilles <er...@apache.org>
Authored: Sat Sep 10 16:25:44 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Sat Sep 10 16:25:44 2016 +0200

----------------------------------------------------------------------
 .../rng/internal/source32/KISSRandom.java       | 15 ++------
 .../internal/source32/MultiplyWithCarry256.java | 15 ++------
 .../commons/rng/internal/util/SeedFactory.java  | 36 ++++++++++++++++++++
 3 files changed, 40 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/2471de81/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java b/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
index 674d803..62ae60b 100644
--- a/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
+++ b/src/main/java/org/apache/commons/rng/internal/source32/KISSRandom.java
@@ -17,6 +17,7 @@
 package org.apache.commons.rng.internal.source32;
 
 import org.apache.commons.rng.internal.util.NumberFactory;
+import org.apache.commons.rng.internal.util.SeedFactory;
 
 /**
  * Port from Marsaglia's <a href="http://www.cse.yorku.ca/~oz/marsaglia-rng.html">
@@ -78,20 +79,8 @@ public class KISSRandom extends IntProvider {
     private void setSeedInternal(int[] seed) {
         // Reset the whole state of this RNG (i.e. the 4 state variables).
         // Seeding procedure is not part of the reference code.
-
         final int[] tmp = new int[SEED_SIZE];
-        System.arraycopy(seed, 0, tmp, 0, Math.min(seed.length, tmp.length));
-
-        if (seed.length < SEED_SIZE) {
-            for (int i = seed.length; i < SEED_SIZE; i++) {
-                tmp[i] = 26021969 * i;
-            }
-            for (int i = SEED_SIZE - 1; i > seed.length; i--) {
-                tmp[i] ^= tmp[SEED_SIZE - i - 1];
-            }
-
-            tmp[seed.length] = 0x80000000; // Ensuring non-zero initial array.
-        }
+        SeedFactory.fillState(tmp, seed);
 
         z = tmp[0];
         w = tmp[1];

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/2471de81/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java b/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
index 8ed0331..58b48d1 100644
--- a/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
+++ b/src/main/java/org/apache/commons/rng/internal/source32/MultiplyWithCarry256.java
@@ -18,6 +18,7 @@ package org.apache.commons.rng.internal.source32;
 
 import java.util.Arrays;
 import org.apache.commons.rng.internal.util.NumberFactory;
+import org.apache.commons.rng.internal.util.SeedFactory;
 
 /**
  * Port from Marsaglia's <a href="https://en.wikipedia.org/wiki/Multiply-with-carry">
@@ -87,20 +88,8 @@ public class MultiplyWithCarry256 extends IntProvider {
     private void setSeedInternal(int[] seed) {
         // Reset the whole state of this RNG (i.e. "state" and "index").
         // Seeding procedure is not part of the reference code.
-
         final int[] tmp = new int[SEED_SIZE];
-        System.arraycopy(seed, 0, tmp, 0, Math.min(seed.length, tmp.length));
-
-        if (seed.length < SEED_SIZE) {
-            for (int i = seed.length; i < SEED_SIZE; i++) {
-                tmp[i] = 26021969 * i;
-            }
-            for (int i = SEED_SIZE - 1; i > seed.length; i--) {
-                tmp[i] ^= tmp[SEED_SIZE - i - 1];
-            }
-
-            tmp[seed.length] = 0x80000000; // Ensuring non-zero initial array.
-        }
+        SeedFactory.fillState(tmp, seed);
 
         // First element of the "seed" is the initial "carry".
         final int c = tmp[0];

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/2471de81/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 fe9e66c..047e9f0 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
@@ -98,6 +98,42 @@ public class SeedFactory {
     }
 
     /**
+     * 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(int[] state,
+                                 int[] seed) {
+        final int stateSize = state.length;
+        final int seedSize = seed.length;
+        System.arraycopy(seed, 0, state, 0, Math.min(seedSize, stateSize));
+
+        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[seedSize] = 0x80000000; // Ensuring non-zero initial array.
+        }
+    }
+
+    /**
      * Creates an array of numbers for use as a seed.
      *
      * @param n Size of the array to create.