[ 
https://issues.apache.org/jira/browse/MATH-899?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13498817#comment-13498817
 ] 

Thomas Neidhart commented on MATH-899:
--------------------------------------

Regarding sync (see embedded code from next(int) method).

This part has to be sync'ed, as it not only relies on the internal variable 
mti, it also modifies the internal array mt:

{noformat}
        if (mti >= N) { // generate N words at one time
            int mtNext = mt[0];
            for (int k = 0; k < N - M; ++k) {
                int mtCurr = mtNext;
                mtNext = mt[k + 1];
                y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff);
                mt[k] = mt[k + M] ^ (y >>> 1) ^ MAG01[y & 0x1];
            }
            for (int k = N - M; k < N - 1; ++k) {
                int mtCurr = mtNext;
                mtNext = mt[k + 1];
                y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff);
                mt[k] = mt[k + (M - N)] ^ (y >>> 1) ^ MAG01[y & 0x1];
            }
            y = (mtNext & 0x80000000) | (mt[0] & 0x7fffffff);
            mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAG01[y & 0x1];

            mti = 0;
        }

        y = mt[mti++];
{noformat}

This part does not need to be sync'ed:

{noformat}
        // tempering
        y ^=  y >>> 11;
        y ^= (y <<   7) & 0x9d2c5680;
        y ^= (y <<  15) & 0xefc60000;
        y ^=  y >>> 18;

        return y >>> (32 - bits);
{noformat}

So, have a more fine-grained sync would not be of much benefit imho. Maybe 
adding a sync'ed wrapper of RandomGenerator makes sense, which takes another 
RandomGenerator as input?

{quote}
To do that is a huge effort. For now, users must assume that no class (except 
immutable ones) is thread-safe.
{quote}

for all of CM indeed, for just the RNGs this would be feasible. I think the 
problem here also comes from the fact that the JDK RNG is thread-safe, so 
people may tend to assume the same is true for CM RNGs.
                
> A random crash of MersenneTwister random generator
> --------------------------------------------------
>
>                 Key: MATH-899
>                 URL: https://issues.apache.org/jira/browse/MATH-899
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 3.0
>         Environment: Windows 7, JDK 1.7.05
>            Reporter: Alexander Nozik
>            Priority: Minor
>
> There is a very small probability that MersenneTwister generator gives a 
> following error: 
> java.lang.ArrayIndexOutOfBoundsException: 624
> in MersenneTwister.java line 253
> The error is completely random and its probability is about 1e-8.
> UPD: The problem most probably arises only in multy-thread mode.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to