[
https://issues.apache.org/jira/browse/RNG-115?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16921045#comment-16921045
]
Alex D Herbert commented on RNG-115:
------------------------------------
Fixed this with [PR 65|https://github.com/apache/commons-rng/pull/65].
Note that the PR saves the state size to the byte array containing the state.
The size is prepended so when reading the state the first 4 bytes can be
interpreted as the state size. The rest of the state can then be read.
An alternative is the simple fix to change the private stateSize field to
static:
{code:java}
private int stateSize;
// Change to ...
private static int stateSize;
{code}
This would work within the same JVM. If the state is written to disc and then
loaded in a new JVM then the restore would not work as the static field would
have no value until the first call the saveState().
> JDKRandom to allow restore state when saved from a different instance
> ---------------------------------------------------------------------
>
> Key: RNG-115
> URL: https://issues.apache.org/jira/browse/RNG-115
> Project: Commons RNG
> Issue Type: Bug
> Components: core
> Affects Versions: 1.2
> Reporter: Alex D Herbert
> Assignee: Alex D Herbert
> Priority: Minor
> Time Spent: 10m
> Remaining Estimate: 0h
>
> Currently the size of the serialized state of the java.util.Random used by
> JDKRandom is saved to the instance when the state is saved. Thus the state
> cannot be used to restore a different instance of the same class. This breaks
> the contract of the RestorableUniformRandomProvider as the state should be
> applicable to a different instance of the same class.
> Fix this test to work:
> {code:java}
> @Test
> public void testRestoreToNewInstance() {
> final long seed = 8796746234L;
> final JDKRandom rng1 = new JDKRandom(seed);
> final JDKRandom rng2 = new JDKRandom(seed + 1);
> final RandomProviderState state = rng1.saveState();
> rng2.restoreState(state);
> final int numRepeats = 1000;
> for (int r = 0; r < numRepeats; r++) {
> Assert.assertEquals(r + " nextInt", rng1.nextInt(), rng2.nextInt());
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.3.2#803003)