You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Remi Arntzen (JIRA)" <ji...@apache.org> on 2006/07/31 05:50:13 UTC

[jira] Created: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

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

                 Key: MATH-153
                 URL: http://issues.apache.org/jira/browse/MATH-153
             Project: Commons Math
          Issue Type: Bug
    Affects Versions: Nightly Builds, 1.1 Final
            Reporter: Remi Arntzen
            Priority: Critical


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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[jira] Commented: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

Posted by "Remi Arntzen (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/MATH-153?page=comments#action_12424472 ] 
            
Remi Arntzen commented on MATH-153:
-----------------------------------

once Math-154 (MathUtils addAndCheck and subAndCheck for long values)
is commited nextLong(long, long) could become

public long nextLong(long lower, long upper) {
    if (lower >= upper) {
        throw new IllegalArgumentException(
                "upper bound must be > lower bound");
    }
    RandomGenerator rand = getRan();
    boolean overFlow = false;
    try {
        MathUtils.subAndCheck(upper, lower);
    } catch (ArithmeticException thrown) {
        overFlow = true;
    }
    
    long returnValue;
    
    if (upper - lower == Long.MAX_VALUE) {
        //upper - lower + 1 = Long.MAX_VALUE + 1 = *overflow error*;
        overFlow = true;
    }
    
    if (!overFlow) {
        returnValue = lower + (long) (rand.nextDouble() * (upper -
                                                           lower + 1)
                                                          );
    } else {
        returnValue = rand.nextLong();
        while (returnValue > upper || returnValue < lower) {
            returnValue = rand.nextLong();
        }
    }
    return returnValue;
}

> RandomDataImpl nextInt(int, int) nextLong(long, long)
> -----------------------------------------------------
>
>                 Key: MATH-153
>                 URL: http://issues.apache.org/jira/browse/MATH-153
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: Nightly Builds, 1.1 Final
>            Reporter: Remi Arntzen
>            Priority: Critical
>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[jira] Commented: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

Posted by "Brent Worden (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/MATH-153?page=comments#action_12426210 ] 
            
Brent Worden commented on MATH-153:
-----------------------------------

I think we can avoid the overflow conditions simply by distributing the multiplication of the random value.  With this, the method body would become:

double r = rand.nextDouble();
return (int)((r * upper) + ((1.0 - r) * lower) + r);


> RandomDataImpl nextInt(int, int) nextLong(long, long)
> -----------------------------------------------------
>
>                 Key: MATH-153
>                 URL: http://issues.apache.org/jira/browse/MATH-153
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: Nightly Builds, 1.1 Final
>            Reporter: Remi Arntzen
>            Priority: Critical
>             Fix For: 1.2 Final
>
>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[jira] Resolved: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

Posted by "Brent Worden (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/MATH-153?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Brent Worden resolved MATH-153.
-------------------------------

       Resolution: Fixed
    Fix Version/s: 1.2

SVN 525842: Corrected nextInt and nextLong to handle wide value ranges.

> RandomDataImpl nextInt(int, int) nextLong(long, long)
> -----------------------------------------------------
>
>                 Key: MATH-153
>                 URL: https://issues.apache.org/jira/browse/MATH-153
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 1.1, Nightly Builds
>            Reporter: Remi Arntzen
>            Priority: Critical
>             Fix For: 1.2
>
>         Attachments: diff.txt, Test.diff
>
>
> 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.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[jira] Commented: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/MATH-153?page=comments#action_12426064 ] 
            
Phil Steitz commented on MATH-153:
----------------------------------

Agree this is a bug and should be fixed.   Thanks for reporting this issue.

Using the added method in MATH-154 to detect overflow in the long case is good.

There should be a way to avoid the while loop at the end of the long impl.

We also need test cases to close this.


> RandomDataImpl nextInt(int, int) nextLong(long, long)
> -----------------------------------------------------
>
>                 Key: MATH-153
>                 URL: http://issues.apache.org/jira/browse/MATH-153
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: Nightly Builds, 1.1 Final
>            Reporter: Remi Arntzen
>            Priority: Critical
>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[jira] Updated: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

Posted by "Remi Arntzen (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/MATH-153?page=all ]

Remi Arntzen updated MATH-153:
------------------------------

    Attachment: Test.diff

> RandomDataImpl nextInt(int, int) nextLong(long, long)
> -----------------------------------------------------
>
>                 Key: MATH-153
>                 URL: http://issues.apache.org/jira/browse/MATH-153
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: Nightly Builds, 1.1 Final
>            Reporter: Remi Arntzen
>            Priority: Critical
>             Fix For: 1.2 Final
>
>         Attachments: diff.txt, Test.diff
>
>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[jira] Updated: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long)

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/MATH-153?page=all ]

Phil Steitz updated MATH-153:
-----------------------------

    Fix Version/s: 1.2 Final

> RandomDataImpl nextInt(int, int) nextLong(long, long)
> -----------------------------------------------------
>
>                 Key: MATH-153
>                 URL: http://issues.apache.org/jira/browse/MATH-153
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: Nightly Builds, 1.1 Final
>            Reporter: Remi Arntzen
>            Priority: Critical
>             Fix For: 1.2 Final
>
>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org