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

RandomDataImpl nextInt(int, int) nextLong(long, long)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.1
    • 1.2
    • None
    • None

    Description

      RandomDataImpl.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE) suffers from overflow errors.

      change
      return lower + (int) (rand.nextDouble() * (upper - lower + 1));
      to
      return (int) (lower + (long) (rand.nextDouble()*((long) upper - lower + 1)));

      additional checks must be made for the nextlong(long, long) method.
      At first I thought about using MathUtils.subAndCheck(long, long) but there is only an int version avalible, and the problem is that subAndCheck internaly uses long values to check for overflow just as my previous channge proposes. The only thing I can think of is using a BigInteger to check for the number of bits required to express the difference.

      Attachments

        1. diff.txt
          0.9 kB
          Remi Arntzen
        2. Test.diff
          3 kB
          Remi Arntzen

        Activity

          People

            Unassigned Unassigned
            remi_arntzen Remi Arntzen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: