Skip to content

Commit

Permalink
Avoid code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles committed Sep 10, 2016
1 parent fe6ec47 commit 2471de8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -78,20 +79,8 @@ protected void setStateInternal(byte[] s) {
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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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">
Expand Down Expand Up @@ -87,20 +88,8 @@ protected void setStateInternal(byte[] s) {
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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,42 @@ public static long[] createLongArray(int n) {
return createLongArray(n, SEED_GENERATOR, new Object());
}

/**
* 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.
*
Expand Down

0 comments on commit 2471de8

Please sign in to comment.