Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 10470 invoked from network); 7 Aug 2006 13:23:58 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 7 Aug 2006 13:23:58 -0000 Received: (qmail 2297 invoked by uid 500); 7 Aug 2006 13:23:56 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 2224 invoked by uid 500); 7 Aug 2006 13:23:56 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 2213 invoked by uid 99); 7 Aug 2006 13:23:56 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Aug 2006 06:23:56 -0700 X-ASF-Spam-Status: No, hits=1.5 required=10.0 tests=SUBJECT_FUZZY_TION X-Spam-Check-By: apache.org Received: from [209.237.227.198] (HELO brutus.apache.org) (209.237.227.198) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Aug 2006 06:23:55 -0700 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id BED5871429F for ; Mon, 7 Aug 2006 13:21:14 +0000 (GMT) Message-ID: <27468103.1154956874779.JavaMail.jira@brutus> Date: Mon, 7 Aug 2006 06:21:14 -0700 (PDT) From: "Brent Worden (JIRA)" To: commons-dev@jakarta.apache.org Subject: [jira] Commented: (MATH-153) RandomDataImpl nextInt(int, int) nextLong(long, long) In-Reply-To: <16879243.1154317813908.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N [ 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