Skip to content

np.random.binomial produces inconsistent output with same seed in 1.16 -> 1.17 #14522

Closed

Description

@dburkhardt

Reproducing code example:

import numpy as np
np.random.seed(42)

np.random.binomial(1, np.linspace(0, 1, 1000), 1000).sum()

produces 515 on version 1.16.3 and 514 on version 1.17.0+

Error message:

Numpy/Python version information:

1.16.3 3.7.3 (default, Mar 26 2019, 21:43:19)
[GCC 8.2.1 20181127]

and

1.17.2 3.7.3 (default, Mar 26 2019, 21:43:19)
[GCC 8.2.1 20181127]

Activity

  1. WarrenWeckesser commented on Sep 16, 2019

    @WarrenWeckesser
    Member

    Simpler example:

    In [44]: np.__version__
    Out[44]: '1.16.4'
    In [45]: np.random.seed(12345)
    In [46]: np.random.binomial(1, [0, 0.5])
    Out[46]: array([0, 0])
    
    In [60]: np.__version__
    Out[60]: '1.18.0.dev0+33d5f9c'
    In [61]: np.random.seed(12345)
    In [62]: np.random.binomial(1, [0, 0.5])
    Out[62]: array([0, 1])
    
  2. WarrenWeckesser commented on Sep 16, 2019

    @WarrenWeckesser
    Member

    This appears to be the result of an explicit check for p = 0 in the new code:

    if ((n == 0LL) || (p == 0.0f))

    The old code did not have this shortcut, so it would make at least one more call to rk_double(state) than is made in the new code.

  3. charris commented on Sep 16, 2019

    @charris
    Member

    Looks like a regression, the call sequence of the old code should not have been changed unless the values resulted in a hang, in which case it probably wouldn't matter since no one would use those values.

  4. added this to the 1.17.3 release milestone on Sep 16, 2019
  5. WarrenWeckesser commented on Sep 16, 2019

    @WarrenWeckesser
    Member

    @charris, I'm looking into fixing the regression.

  6. charris commented on Sep 16, 2019

    @charris
    Member

    @WarrenWeckesser Great, I was just starting to investigate.

    We do test the generated sequences for many things, but it seems that doesn't catch changes in corner cases. Good thing we no longer make that guarantee :)

  7. added a commit that references this issue on Sep 17, 2019
    ff11e2f
  8. added a commit that references this issue on Sep 22, 2019
    dc441ee
  9. dburkhardt commented on Sep 23, 2019

    @dburkhardt
    Author

    Thank you all for looking into this! I really appreciate the work you put into Numpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions