octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UBsans and rand()


From: Philipp Kutin
Subject: Re: UBsans and rand()
Date: Wed, 14 Aug 2013 16:31:49 +0200

On Sun, Aug 11, 2013 at 5:22 PM, Rik <address@hidden> wrote:
> Does Matlab, in point of fact, accept negative numbers?  Could somebody test 
> that?

Yup, it does. I'll explain some testing I did in a moment.

> And if it does, is it doing anything more than taking the absolute value and 
> then feeding it into the algorithm?

It's more that they consider rand() deprecated because they think some
aspects of calling it are confusing. Specifically, that "state" and
"seed" don't actually mean setting the initial state by seed value and
directly, but denote switching entirely different PRNGs. In fact, they
go as far as to say:

 "These syntaxes referred to different types of generators, and they
will be removed in a future release (...)".

The way I see it is that Octave has it similarly: a legacy RNG
selected with "seed" and the recommended Mersenne Twister ("state" or
"twister").

> Rather than dig deeply into the C++ compiler specifications it might be 
> easier just to restrict the input that Octave accepts.

I only consulted C++11 at the surface for this, but I'd hazard a guess
that what happens with negative seeds is left unspecified. C++11 does
in fact describe the Mersenne Twister, but the input values seem
always to be of unsigned type.

----------

Now to my testing. The function testrand() takes the name of an RNG
and a vector of seeds. Then it prints out the first six values
returned from the requested RNG as a string of letters (for easy
comparison by eye). The results are that both negative, out-of-range,
and NaN/Inf seeds are accepted. Often, large and nonfinite numbers
lead to the same sequence. The function, a driver script, and the
result log are attached.

So IMO, it is sensible to reduce modulo 2^32 as I described in the
other thread, and to initilize using a fixed but arbitrary value in
the case of nonfinites.

--Philipp

Attachment: testrand.m
Description: Binary data

Attachment: testrands.m
Description: Binary data

Attachment: TESTRANDS_R2013a.log
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]