commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles <>
Subject [Math] Setting the state of RNGs (a.k.a. seeding)
Date Mon, 11 Jan 2016 02:09:58 GMT

This post is related to:

Different RNGs have different ways to manage their internal state
(which is updated at each call to the "next()" function).
Some use one, or two, "int" (or "long") values, or arrays (of
varying sizes).

This leads to many signatures for setting that state:

Some families of RNGs could even allow to switch to another member
of that family by resetting yet other data that make up the state.

So this adds up in such a way that it is either incorrect or misleading
to assume that the API advertised by a single interface can represent
all the situations.
A trivial example is an RNG that must implement "setSeed(int[])" (since
it is part of the "RandomGenerator" interface), but only uses a single
"int" to set its state; it will thus probably simply discard all array
elements except the first.
At the opposite, an RNG that has a complex state (say, an array of N
"long" values) will need to provide a procedure to create this array
from the single value that is passed when calling e.g. "setSeed(int)".

I think that it would be more appropriate to advertise the seeding
procedure(s) in separate interfaces, e.g.:

public interface LongSeedable {
   void setSeed(long seed);

public interface IntArraySeedable {
   void setSeed(int[] seed)

Hence RNGs that can indeed initialize, in a "complete" way, their
state from various input types would implement each of the
corresponding interfaces.

Apart from that, we could provide wrappers that allow to call a
non-"natively" implemented "setSeed" method (where the transformation
from, say, "long" to "int[]" is performed by an external procedure):

public interface Long2IntArrayTransformer {
   int[] toIntArray(long seed);

public class SeedableFactory {
   public LongSeedable makeLongSeedable(final IntArraySeedable rng,
                                        final Long2IntArrayTransformer 
c) {
     return new LongSeedable() {
       public void setSeed(long seed) {

   // And similarly for other conversions.

In user code:

// Constructor initializes "native" state.
Well1024a rng = new Well1024a(new int[] { 11, 29 });

// Call nextDouble(), nextInt(), etc.

// Reseed "natively".
rng.setSeed(new int[] { 17, 43 });

// Call nextDouble(), nextInt(), etc.

// Reseed using non-"native" method "setSeed(long)" (assuming that 
// implements "IntArraySeedable" but not "LongSeedable").
// Assuming that "WellSeedTransformer" implements 
SeedableFactory.makeLongSeedable(rng, new 

// Call nextDouble(), nextInt(), etc.

In summary, the "SeedableFactory" methods could create uniform
"views" of dissimilar RNGs, and would allow users to select
a state initialization algorithm independently of the RNG


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message