help-octave
[Top][All Lists]
Advanced

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

Re: Random number generation quirk


From: Michael Pender
Subject: Re: Random number generation quirk
Date: Fri, 28 Feb 2014 11:59:17 -0500

A few billion digits may not be enough to reproduce the problem if you are using rand(..., "double") which is the default.  I found it happens more frequently when using rand(..., "single").

That said, I am not sure it is a bug.  It may be expected behavior for the Mersenne Twister to potentially return values at either one or the other extreme of the range (0.0, 1.0).  I can't find the web page today, but I do remember reading a Matlab documentation page that said for certain values of a and b, the _expression_ x = rand * a + b may return either a or b.  I realize this is Octave and not Matlab, but it is possible the implementation is similar enough that the same bounds can occur.

On Fri, Feb 28, 2014 at 8:20 AM, Jordi Gutiérrez Hermoso <address@hidden> wrote:

I'm unable to reproduce this after getting a few billion random
numbers out of rand. If rand is actually ever giving you exactly 1,
this is a bug. Are you able to see a bug?

    http://hg.savannah.gnu.org/hgweb/octave/file/c579bd4e12c9/liboctave/numeric/randmtzig.c#l400

- Jordi G. H.

I am not sure it is a bug, but I am suspicious of line 396, where it adds 0.5 before performing a single precision division operation since the numerator has been cast to a single precision float.  I would need to experiment to confirm whether that is definitely a problem.  It may reflect my limited understanding of the algorithm used.  Similarly, I do not understand the reason for adding a and b to the constant 0.4 in lines 404-406 to generate a 53-bit random number.  I assume a and b are being shifted into place so when added they form the 53-bits of the random number field, but that doesn't explain the constant offset.

- Mike



On Fri, Feb 28, 2014 at 8:20 AM, Jordi Gutiérrez Hermoso <address@hidden> wrote:
On Thu, 2014-02-27 at 18:56 -0800, mpender wrote:
> This normally returns a number between 1 and 10. But sometimes I get
> some difficult to trace array index out of bound errors when I use
> the computed value as an array index. I suspect that sometimes the
> rand function returns exactly 1.0 and then round (1.0 * 10 + 0.5) =
> 11 , which results in my index out of bounds condition.

I'm unable to reproduce this after getting a few billion random
numbers out of rand. If rand is actually ever giving you exactly 1,
this is a bug. Are you able to see a bug?

    http://hg.savannah.gnu.org/hgweb/octave/file/c579bd4e12c9/liboctave/numeric/randmtzig.c#l400

- Jordi G. H.





reply via email to

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