bug-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug classpath/22971] Random number generation creates invalid values.


From: gcc-bugzilla at gcc dot gnu dot org
Subject: [Bug classpath/22971] Random number generation creates invalid values.
Date: 16 Oct 2005 01:27:49 -0000

While trying to run Azureus on latest SableVM snapshot( 2005-05-14, using
classpath 0.15 ) I came across a StringOutOfBoundsException and managed to
track the error to the Random number generation, it creates numbers
higher/lower than what it's supposed to( 2147483647 and -2147483647 )

Here's a piece of code that causes this problem, the code inside the main has
been taken from Azureus source code and modified with the System.out so you can
see the output:

public class Test
{
   public static void main( String args[] )
   {
      String uniqueId ="";
      String chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

      for(int i = 0 ; i < 20 ; i++) {
         int pos = (int) ( Math.random() * chars.length());
         System.out.println( pos );
         uniqueId += chars.charAt(pos);

      }
   }
}

Running this piece of code with classpath 0.13 generates the correct output,
random numbers less than chars.length()


------- Comment #1 from from-classpath at savannah dot gnu dot org  2005-06-02 
16:18 -------
I haven't been able to reproduce this. Could you give some info on what
platform you're running?


------- Comment #2 from from-classpath at savannah dot gnu dot org  2005-06-02 
21:40 -------
This was run on Debian Sarger 3.1 and SUSE 9.2, both with a 2.6.8-24 kernel.
The reason you might have not been able to reproduce is because I was actually
running this with classpath 0.14 and not 0.15, my apologies for this, I was led
to believe I was using the latest release. I'm really sorry if I wasted your
time.


------- Comment #3 from from-classpath at savannah dot gnu dot org  2005-06-02 
22:34 -------
Well, that doesn't really change things; Neither java.lang.Math nor the
java.util.Random (which it uses for Math.random()) have been changed since long
before 0.14.

What architecture are you running on? Is it ARM? This seems a lot like Bug
#11058, also reported on SableVM, which had some problems with doubles. 

The testcase for that bug erroneously prints the same value you mentioned:
2147483647, which can't be a coincidence. Unfortunately I don't know the exact
cause of that bug either.


------- Comment #4 from from-classpath at savannah dot gnu dot org  2005-06-03 
23:12 -------
It's an i686. Anyway, I did notice those classes haven't been changed in quite
a while so the problem must reside somewhere else, the VM maybe? 

Thanks for the help.


------- Comment #5 from from-classpath at savannah dot gnu dot org  2005-06-04 
17:08 -------
Hmm.. ok. Then I'm guessing it's a VM problem, then.

Of course that number isn't so significant in itself, it's just 0x7FFFFFFF.

Try printing the double values before and after casting to int, and see what
happens.


------- Comment #6 from from-classpath at savannah dot gnu dot org  2005-06-05 
12:06 -------
I see, here's the output before and after casting to int. I also tried casting
to long, short and float, when casting to long and float it brings the correct
output, but short brings some 0's and -1 instead of the 2147483648 and
-2147483648.


3.2680927015788903
-2147483648

48.676101959694606
48

15.603303272040826
15

45.18679449911908
-2147483648

50.183405249914465
-2147483648

57.99931603444814
57

38.57864976574081
38

36.30425765078901
-2147483648

31.345613996020415
31

44.611820742164916
44

24.0072916995037
-2147483648

5.759369039050663
5

45.17598697068571
45

56.54270822097954
2147483647

37.43793782159153
2147483647

8.445844681955768
8

41.26446978250817
41

0.0966193579536061
-2147483648

50.92214798727264
-2147483648

16.164099315461403
2147483647


------- Comment #7 from from-classpath at savannah dot gnu dot org  2005-06-05 
18:33 -------
Ok, well then this is a SableVM bug. 


------- Comment #8 from from-classpath at savannah dot gnu dot org  2005-06-07 
20:17 -------
Ok, this is apparently accidentally introduced in a SableVM patch on May 9 and
is fixed now, so I'm closing this.


------- Comment #9 from from-classpath at savannah dot gnu dot org  2005-06-07 
20:24 -------
Actually, it has been fixed in revision 4092 of SableVM's trunk.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22971





reply via email to

[Prev in Thread] Current Thread [Next in Thread]