commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Herbert <alex.d.herb...@gmail.com>
Subject Re: [rng] Split and Jump functions
Date Mon, 10 Jun 2019 12:55:03 GMT
I've submitted a PR with the functionality discussed in this thread for 
JumpableUniformRandomProvider [1].

Some notes:

- The jump must return a copy.

This requires a bit of code all the way up to the base implementations 
of IntProvider and LongProvider to copy the current state. I've done 
this explicitly with copy constructors to avoid using clone() [2].


- The jump has a few requirements that are tested:

1. The copy is a new instance of the same class.

2. The sequence output from the original RNG after the jump must match 
the reference implementation output.

3. The sequence output from the copy must match what would have been 
output from the original RNG.


- The default implementations of the providers have cached state

To ensure the state is copied this is tested by comparing the state of 
the copy with the state of the original pre-jump. This does not require 
any knowledge of the original state. Just that the state can be accesses 
using RestorableUniformRandomProvider.

To ensure that the state is cleared in the original RNG post-jump 
requires that the state pre-jump is not zero and the state after the 
jump is zero.

One method is to: create a generator, call nextInt or nextBoolean to 
make it cache some state, do the jump; compare the output of nextInt or 
nextBoolean to the copy and test it is different. However this requires 
that the initial state is new (all zero bytes) and so the test must be 
called in every test class for each generator by passing a new instance.

Alternatively a test is made using the state from 
RestorableUniformRandomProvider. The size of the default state is known 
and post-jump is checked to ensure it is zero. This test does not 
require the pre-jump state to be zero and thus can be put into a 
parametric test for all the JumpableUniformRandomProviders.

So the tests to check the output sequence post-jump are in the 
individual tests for each provider and a JumpableProvidersParametricTest 
tests the general requirements of a Jumpable RNG. This ensures all 
generators in the library pass tests to ensure common functionality (the 
copy is a new instance, the state is the same as the original, the 
post-jump state of the original has reset the cached state).


- Some providers are LongJumpable

This will be added for RNG-98 and I created this PR with future 
functionality in mind. The jump function to advance the state is named 
performJump. For those without a LongJump the coefficients are not 
passed as a parameter and can be hard coded. For any generator with a 
LongJump function the computation of the jump in the performJump 
function accepts an array of coefficients.  This will be called with the 
appropriate long jump coefficients to implement LongJumpable. The rest 
of the code will be unchanged and new tests can be added to check the 
long jump.


Comments are welcomed.

Alex


[1] https://github.com/apache/commons-rng/pull/48

[2] https://www.artima.com/intv/bloch13.html




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


Mime
View raw message