Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-724

RandomDataImpl.nextInt does not distribute uniformly for negative lower bound

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0, 1.1, 1.2, 2.0, 2.1, 2.2
    • 3.0
    • None
    • None

    Description

      When using the RandomDataImpl.nextInt function to get a uniform sample in a [lower, upper] interval, when the lower value is less than zero, the output is not uniformly distributed, as the lowest value is practically never returned.

      See the attached NextIntUniformTest.java file. It uses a [-3, 5] interval. For several values between 0 and 1, testNextIntUniform1 prints the return value of RandomDataImpl.nextInt (as double and as int). We see that -2 through 5 are returned several times. The -3 value however, is only returned for 0.0, and is thus under-respresented in the integer samples. The output of test method testNextIntUniform2 also clearly shows that value -3 is never sampled.

      Attachments

        1. math-724.patch
          1 kB
          Dennis Hendriks
        2. math-724-v2.patch
          2 kB
          Dennis Hendriks
        3. math-724-v3.patch
          2 kB
          Dennis Hendriks
        4. NextIntTest3.java
          6 kB
          Dennis Hendriks
        5. NextIntUniformTest.java
          3 kB
          Dennis Hendriks
        6. NextUniformTest3.java
          6 kB
          Dennis Hendriks

        Activity

          People

            Unassigned Unassigned
            dhendriks Dennis Hendriks
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: