MATH-153: Corrected nextInt and nextLong to handle wide value ranges.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@525842 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brent Worden 2007-04-05 15:19:57 +00:00
parent 55b2cc8c03
commit 409d56d206
3 changed files with 19 additions and 2 deletions

View File

@ -167,7 +167,8 @@ public class RandomDataImpl implements RandomData, Serializable {
("upper bound must be > lower bound");
}
RandomGenerator rand = getRan();
return lower + (int) (rand.nextDouble() * (upper - lower + 1));
double r = rand.nextDouble();
return (int)((r * upper) + ((1.0 - r) * lower) + r);
}
/**
@ -184,7 +185,8 @@ public class RandomDataImpl implements RandomData, Serializable {
("upper bound must be > lower bound");
}
RandomGenerator rand = getRan();
return lower + (long) (rand.nextDouble() * (upper - lower + 1));
double r = rand.nextDouble();
return (long)((r * upper) + ((1.0 - r) * lower) + r);
}
/**

View File

@ -58,6 +58,18 @@ public class RandomDataTest extends RetryTestCase {
return suite;
}
public void testNextIntExtremeValues() {
int x = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE);
int y = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE);
assertFalse(x == y);
}
public void testNextLongExtremeValues() {
long x = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE);
long y = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE);
assertFalse(x == y);
}
/** test dispersion and failure modes for nextInt() */
public void testNextInt() {
try {

View File

@ -71,6 +71,9 @@ Commons Math Release Notes</title>
Modified getSumSquaredErrors method in SimpleRegression to always
return a non-negative result.
</action>
<action dev="brentworden" type="fix" issue="MATH-153" due-to="Remi Arntzen">
Corrected nextInt and nextLong to handle wide value ranges.
</action>
</release>
<release version="1.1" date="2005-12-17"
description="This is a maintenance release containing bug fixes and enhancements.