Skip to content

Commit a14cf2e

Browse files
committed
RNG-192: Remove use of overriddable instance method for state
initialisation
1 parent 081a7b6 commit a14cf2e

16 files changed

Lines changed: 31 additions & 95 deletions

commons-rng-core/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<commons.jacoco.instructionRatio>0.99</commons.jacoco.instructionRatio>
5757
<commons.jacoco.lineRatio>0.99</commons.jacoco.lineRatio>
5858
<!-- Change from commons-parent of 1.0 as method handles to JDK Math cannot always be executed -->
59+
<commons.jacoco.branchRatio>0.99</commons.jacoco.branchRatio>
5960
<commons.jacoco.methodRatio>0.99</commons.jacoco.methodRatio>
6061
<commons.jacoco.complexityRatio>0.99</commons.jacoco.complexityRatio>
6162

commons-rng-core/src/main/java/org/apache/commons/rng/core/BaseProvider.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ protected void setStateInternal(byte[] state) {
174174
* </li>
175175
* </ol>
176176
*
177+
* <p>Note: It is not recommended to use this method from a constructor
178+
* as it can be overridden by subclasses possibly leading to unexpected behaviour.
179+
* Future versions may remove this method, or change it to static.
180+
*
177181
* @param state State. Must be allocated.
178182
* @param seed Seed. Cannot be null.
179183
*/
@@ -185,7 +189,7 @@ protected void fillState(int[] state,
185189

186190
if (seedSize < stateSize) {
187191
for (int i = seedSize; i < stateSize; i++) {
188-
state[i] = (int) scrambleWell(state[i - seed.length], i);
192+
state[i] = (int) scrambleWell(state[i - seedSize], i);
189193
}
190194
}
191195
}
@@ -205,6 +209,10 @@ protected void fillState(int[] state,
205209
* </li>
206210
* </ol>
207211
*
212+
* <p>Note: It is not recommended to use this method from a constructor
213+
* as it can be overridden by subclasses possibly leading to unexpected behaviour.
214+
* Future versions may remove this method, or change it to static.
215+
*
208216
* @param state State. Must be allocated.
209217
* @param seed Seed. Cannot be null.
210218
*/
@@ -216,7 +224,7 @@ protected void fillState(long[] state,
216224

217225
if (seedSize < stateSize) {
218226
for (int i = seedSize; i < stateSize; i++) {
219-
state[i] = scrambleWell(state[i - seed.length], i);
227+
state[i] = scrambleWell(state[i - seedSize], i);
220228
}
221229
}
222230
}

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/AbstractPcg6432.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ abstract class AbstractPcg6432 extends IntProvider {
9494
* is discarded by left shift and the increment is set to odd.</p>
9595
*/
9696
AbstractPcg6432(long[] seed) {
97-
if (seed.length < SEED_SIZE) {
98-
final long[] tmp = new long[SEED_SIZE];
99-
fillState(tmp, seed);
100-
setSeedInternal(tmp);
101-
} else {
102-
setSeedInternal(seed);
103-
}
97+
setSeedInternal(extendSeed(seed, SEED_SIZE));
10498
}
10599

106100
/**

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/AbstractXoRoShiRo64.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@ abstract class AbstractXoRoShiRo64 extends IntProvider {
4646
* set. A seed containing all zeros will create a non-functional generator.
4747
*/
4848
AbstractXoRoShiRo64(int[] seed) {
49-
if (seed.length < SEED_SIZE) {
50-
final int[] state = new int[SEED_SIZE];
51-
fillState(state, seed);
52-
setState(state);
53-
} else {
54-
setState(seed);
55-
}
49+
setState(extendSeed(seed, SEED_SIZE));
5650
}
5751

5852
/**

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/AbstractXoShiRo128.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ abstract class AbstractXoShiRo128 extends IntProvider implements LongJumpableUni
6161
* set. A seed containing all zeros will create a non-functional generator.
6262
*/
6363
AbstractXoShiRo128(int[] seed) {
64-
if (seed.length < SEED_SIZE) {
65-
final int[] state = new int[SEED_SIZE];
66-
fillState(state, seed);
67-
setState(state);
68-
} else {
69-
setState(seed);
70-
}
64+
setState(extendSeed(seed, SEED_SIZE));
7165
}
7266

7367
/**

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/DotyHumphreySmallFastCounting32.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ public class DotyHumphreySmallFastCounting32 extends IntProvider {
4949
* be used; if smaller, the remaining elements will be automatically set.
5050
*/
5151
public DotyHumphreySmallFastCounting32(int[] seed) {
52-
if (seed.length < SEED_SIZE) {
53-
final int[] state = new int[SEED_SIZE];
54-
fillState(state, seed);
55-
setSeedInternal(state);
56-
} else {
57-
setSeedInternal(seed);
58-
}
52+
setSeedInternal(extendSeed(seed, SEED_SIZE));
5953
}
6054

6155
/**

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/KISSRandom.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ protected void setStateInternal(byte[] s) {
8181
private void setSeedInternal(int[] seed) {
8282
// Reset the whole state of this RNG (i.e. the 4 state variables).
8383
// Filling procedure is not part of the reference code.
84-
final int[] tmp = new int[SEED_SIZE];
85-
fillState(tmp, seed);
84+
final int[] tmp = extendSeed(seed, SEED_SIZE);
8685

8786
z = tmp[0];
8887
w = tmp[1];

commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/MultiplyWithCarry256.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ protected void setStateInternal(byte[] s) {
9191
private void setSeedInternal(int[] seed) {
9292
// Reset the whole state of this RNG (i.e. "state" and "index").
9393
// Filling procedure is not part of the reference code.
94-
final int[] tmp = new int[SEED_SIZE];
95-
fillState(tmp, seed);
94+
final int[] tmp = extendSeed(seed, SEED_SIZE);
9695

9796
// First element of the "seed" is the initial "carry".
9897
final int c = tmp[0];

commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/AbstractXoRoShiRo1024.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ abstract class AbstractXoRoShiRo1024 extends LongProvider implements LongJumpabl
6262
* set. A seed containing all zeros will create a non-functional generator.
6363
*/
6464
AbstractXoRoShiRo1024(long[] seed) {
65-
setSeedInternal(seed);
65+
final long[] tmp = extendSeed(seed, SEED_SIZE);
66+
System.arraycopy(tmp, 0, state, 0, SEED_SIZE);
6667
}
6768

6869
/**
@@ -98,18 +99,6 @@ protected void setStateInternal(byte[] s) {
9899
super.setStateInternal(c[1]);
99100
}
100101

101-
/**
102-
* Seeds the RNG.
103-
*
104-
* @param seed Seed.
105-
*/
106-
private void setSeedInternal(long[] seed) {
107-
// Reset the whole state of this RNG (i.e. "state" and "index").
108-
// Filling procedure is not part of the reference code.
109-
fillState(state, seed);
110-
index = 0;
111-
}
112-
113102
/** {@inheritDoc} */
114103
@Override
115104
public long next() {

commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/AbstractXoRoShiRo128.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,7 @@ abstract class AbstractXoRoShiRo128 extends LongProvider implements LongJumpable
5757
* set. A seed containing all zeros will create a non-functional generator.
5858
*/
5959
AbstractXoRoShiRo128(long[] seed) {
60-
if (seed.length < SEED_SIZE) {
61-
final long[] state = new long[SEED_SIZE];
62-
fillState(state, seed);
63-
setState(state);
64-
} else {
65-
setState(seed);
66-
}
60+
setState(extendSeed(seed, SEED_SIZE));
6761
}
6862

6963
/**

0 commit comments

Comments
 (0)