You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Greg Sterijevski <gs...@gmail.com> on 2011/08/29 07:17:26 UTC

[math] MersenneTwister question

Hello All,

In the MersenneTwister implementation in commons, I notice the following:

    @Override
    public void setSeed(int seed) {
        // we use a long masked by 0xffffffffL as a poor man unsigned int
        long longMT = seed;
        mt[0]= (int) longMT;
        for (mti = 1; mti < N; ++mti) {
            // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
            // initializer from the 2002-01-09 C version by Makoto Matsumoto
            longMT = (1812433253l * (longMT ^ (longMT >> 30)) + mti) &
0xffffffffL;
            mt[mti]= (int) longMT;
        }
    }

While probably not a big deal, shouldn't the initial element in the array mt
be 'anded' by 0xffffffffL?

mt[0]= (int) longMT & 0xffffffffL;

Thanks,

-Greg

Re: [math] MersenneTwister question

Posted by Greg Sterijevski <gs...@gmail.com>.
Yes, I think this is a good idea. I will do it.

On Tue, Aug 30, 2011 at 5:37 AM, sebb <se...@gmail.com> wrote:

> On 29 August 2011 07:11, Greg Sterijevski <gs...@gmail.com> wrote:
> > My apologies, I was looking at the original c code and neglected to
> notice
> > the cast.
>
> Perhaps add a comment to the cast to make it clearer?
>
> > On Mon, Aug 29, 2011 at 12:56 AM, Greg Sterijevski
> > <gs...@gmail.com>wrote:
> >
> >> Yes, you seem to have a good point. My bad.
> >>
> >> On Mon, Aug 29, 2011 at 12:50 AM, Ted Dunning <ted.dunning@gmail.com
> >wrote:
> >>
> >>> Why?
> >>>
> >>> Isn't that what casting as an (int) does?
> >>>
> >>> On Sun, Aug 28, 2011 at 10:17 PM, Greg Sterijevski
> >>> <gs...@gmail.com>wrote:
> >>>
> >>> > While probably not a big deal, shouldn't the initial element in the
> >>> array
> >>> > mt
> >>> > be 'anded' by 0xffffffffL?
> >>> >
> >>> > mt[0]= (int) longMT & 0xffffffffL;
> >>> >
> >>>
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [math] MersenneTwister question

Posted by Greg Sterijevski <gs...@gmail.com>.
Added short comment. -Greg

On Tue, Aug 30, 2011 at 5:37 AM, sebb <se...@gmail.com> wrote:

> On 29 August 2011 07:11, Greg Sterijevski <gs...@gmail.com> wrote:
> > My apologies, I was looking at the original c code and neglected to
> notice
> > the cast.
>
> Perhaps add a comment to the cast to make it clearer?
>
> > On Mon, Aug 29, 2011 at 12:56 AM, Greg Sterijevski
> > <gs...@gmail.com>wrote:
> >
> >> Yes, you seem to have a good point. My bad.
> >>
> >> On Mon, Aug 29, 2011 at 12:50 AM, Ted Dunning <ted.dunning@gmail.com
> >wrote:
> >>
> >>> Why?
> >>>
> >>> Isn't that what casting as an (int) does?
> >>>
> >>> On Sun, Aug 28, 2011 at 10:17 PM, Greg Sterijevski
> >>> <gs...@gmail.com>wrote:
> >>>
> >>> > While probably not a big deal, shouldn't the initial element in the
> >>> array
> >>> > mt
> >>> > be 'anded' by 0xffffffffL?
> >>> >
> >>> > mt[0]= (int) longMT & 0xffffffffL;
> >>> >
> >>>
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [math] MersenneTwister question

Posted by sebb <se...@gmail.com>.
On 29 August 2011 07:11, Greg Sterijevski <gs...@gmail.com> wrote:
> My apologies, I was looking at the original c code and neglected to notice
> the cast.

Perhaps add a comment to the cast to make it clearer?

> On Mon, Aug 29, 2011 at 12:56 AM, Greg Sterijevski
> <gs...@gmail.com>wrote:
>
>> Yes, you seem to have a good point. My bad.
>>
>> On Mon, Aug 29, 2011 at 12:50 AM, Ted Dunning <te...@gmail.com>wrote:
>>
>>> Why?
>>>
>>> Isn't that what casting as an (int) does?
>>>
>>> On Sun, Aug 28, 2011 at 10:17 PM, Greg Sterijevski
>>> <gs...@gmail.com>wrote:
>>>
>>> > While probably not a big deal, shouldn't the initial element in the
>>> array
>>> > mt
>>> > be 'anded' by 0xffffffffL?
>>> >
>>> > mt[0]= (int) longMT & 0xffffffffL;
>>> >
>>>
>>
>>
>

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


Re: [math] MersenneTwister question

Posted by Greg Sterijevski <gs...@gmail.com>.
My apologies, I was looking at the original c code and neglected to notice
the cast.

On Mon, Aug 29, 2011 at 12:56 AM, Greg Sterijevski
<gs...@gmail.com>wrote:

> Yes, you seem to have a good point. My bad.
>
> On Mon, Aug 29, 2011 at 12:50 AM, Ted Dunning <te...@gmail.com>wrote:
>
>> Why?
>>
>> Isn't that what casting as an (int) does?
>>
>> On Sun, Aug 28, 2011 at 10:17 PM, Greg Sterijevski
>> <gs...@gmail.com>wrote:
>>
>> > While probably not a big deal, shouldn't the initial element in the
>> array
>> > mt
>> > be 'anded' by 0xffffffffL?
>> >
>> > mt[0]= (int) longMT & 0xffffffffL;
>> >
>>
>
>

Re: [math] MersenneTwister question

Posted by Greg Sterijevski <gs...@gmail.com>.
Yes, you seem to have a good point. My bad.

On Mon, Aug 29, 2011 at 12:50 AM, Ted Dunning <te...@gmail.com> wrote:

> Why?
>
> Isn't that what casting as an (int) does?
>
> On Sun, Aug 28, 2011 at 10:17 PM, Greg Sterijevski
> <gs...@gmail.com>wrote:
>
> > While probably not a big deal, shouldn't the initial element in the array
> > mt
> > be 'anded' by 0xffffffffL?
> >
> > mt[0]= (int) longMT & 0xffffffffL;
> >
>

Re: [math] MersenneTwister question

Posted by Ted Dunning <te...@gmail.com>.
Why?

Isn't that what casting as an (int) does?

On Sun, Aug 28, 2011 at 10:17 PM, Greg Sterijevski
<gs...@gmail.com>wrote:

> While probably not a big deal, shouldn't the initial element in the array
> mt
> be 'anded' by 0xffffffffL?
>
> mt[0]= (int) longMT & 0xffffffffL;
>