You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Alex Herbert <al...@gmail.com> on 2019/02/13 14:23:19 UTC

[RNG] Allow exceptions from random generator constructors

I am adding a new family of random generators in commons-rng. The 
algorithms do not work when the seed state is all zeros [1]. What would 
be best solution for an API. Here are some options:

1. Throw an exception.

2. Fall-back to a known default seed that works.

3. Allow it and users beware (this is the current behaviour for an 
existing generator in the library with this defect). A generator may be 
created that is broken (e.g. always returns zero).

4. Allow exceptions from the constructor (to prevent misuse) but update 
the recommended way of creating a generator to never fail. This is a 
modification to the commons-rng-simple API method 
RandomSource.create(RandomSource.XXX) to ensure that a seed created by 
the library is never bad for the chosen algorithm.


Option 1 is easy but may present a problem to the user of the library 
who chooses a bad seed (or one is generated badly by chance).

Option 2 ensures that illegal seeds can be used to create good generators.

Option 3 allows creation of bad generators. This is a bug IMO.

Option 4 should allow a user of the recommended API to never get a bad 
generator or an exception. But direct use of the core API requires a 
user beware approach as exceptions can happen is used incorrectly.


I am favouring option 4.


Feedback is welcome.

Alex


[1] Many of the algorithms in the library can accept a zero seed for the 
initial state. This is because they use additions in the algorithm to 
propagate to the next state. The new family uses the Xor operator, bit 
shifts and multiplications. If the initial state is all zero this just 
remains at zero and the generator is broken.



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


Re: [RNG] Allow exceptions from random generator constructors

Posted by Gilles Sadowski <gi...@gmail.com>.
Hello.

Le mer. 13 févr. 2019 à 15:23, Alex Herbert <al...@gmail.com> a écrit :
>
> I am adding a new family of random generators in commons-rng. The
> algorithms do not work when the seed state is all zeros [1]. What would
> be best solution for an API. Here are some options:
>
> 1. Throw an exception.

-1
The core generators should (mis)behave like their reference
implementation
It is useful for experimenting and comparing.

Also the doc mentions that the core generators should not be used
directly.  Non-"advanced" users should stick with the factory methods
in "RandomSource" (where seed auto-generation will highly unlikely
yield a zero state).

>
> 2. Fall-back to a known default seed that works.

-1
Cf. above, this is done in "RandomSource".
Caveat for those who choose their seed.

>
> 3. Allow it and users beware (this is the current behaviour for an
> existing generator in the library with this defect). A generator may be
> created that is broken (e.g. always returns zero).

+1

>
> 4. Allow exceptions from the constructor (to prevent misuse)

-1

> but update
> the recommended way of creating a generator to never fail.

+1
[But it may not be possible to give this warranty.]

> This is a
> modification to the commons-rng-simple API method
> RandomSource.create(RandomSource.XXX) to ensure that a seed created by
> the library is never bad for the chosen algorithm.

AFAIK it is not possible to ensure that a seed is "good".

>
> Option 1 is easy but may present a problem to the user of the library
> who chooses a bad seed (or one is generated badly by chance).
>
> Option 2 ensures that illegal seeds can be used to create good generators.
>
> Option 3 allows creation of bad generators. This is a bug IMO.
>
> Option 4 should allow a user of the recommended API to never get a bad
> generator or an exception. But direct use of the core API requires a
> user beware approach as exceptions can happen is used incorrectly.

The recommended API is "RandomSource" (or use at your own risk);
hence the logic for ensuring, as much as possible, a good RNG must be
done there (if not already implemented by the reference implementation,
as is the case e.g. for "MersenneTwister"[1]).

Best,
Gilles

[1] http://commons.apache.org/proper/commons-rng/commons-rng-core/apidocs/src-html/org/apache/commons/rng/core/source64/MersenneTwister64.html#line.156

>
>
> I am favouring option 4.
>
>
> Feedback is welcome.
>
> Alex
>
>
> [1] Many of the algorithms in the library can accept a zero seed for the
> initial state. This is because they use additions in the algorithm to
> propagate to the next state. The new family uses the Xor operator, bit
> shifts and multiplications. If the initial state is all zero this just
> remains at zero and the generator is broken.

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