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 06:16:14 UTC

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

    [ 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