What is the answer to this strange problem:

• 08-08-2008, 10:10 PM
willemjav
What is the answer to this strange problem:
What is the answer to this strange problem:

I wrote the following random generator, the a and z parameters form the range of random pick.

Code:

```public static int random(int a, int z)  {       int zz = z + a;       int rand = (int)(Math.random()*(zz-1)) + a;       System.out.println("random a " + a + "  z  " + z + "  result  " + rand);       return rand;   }```
When calling the method from the same class everything works fine (the number output is a random pick between 1-10.

Code:

```  public static void main (String args[]) {         new fmEdit();         for (int i = 0; i < 200; i++)       random(1, 10);     }```
But when calling this method from a different class e.g.
vel=ct.random(startgetal,80,100);
(in that class there are a large number of random calls, see bottom part)

the console print shows strange results why?

random a 79 z 394 result 156
random a 1 z 4 result 3
random a 1 z 0 result 1 ??????
random a 80 z 100 result 87
random a 1 z 2 result 2
random a 1 z 0 result 1 ?????
random a 80 z 100 result 110 !!!!!!!!!!!!!
random a 3 z 4 result 6
random a 0 z 127 result 34
random a 50 z 80 result 51
random a 10 z 60 result 53
random a 0 z 127 result 114
random a 50 z 80 result 81 !!!!!!!!!!!!!!!!
random a 10 z 60 result 49
random a 0 z 127 result 68
random a 50 z 80 result 144 !!!!!!!!!!!!
random a 10 z 60 result 26
random a 0 z 127 result 74
random a 50 z 80 result 108
• 08-11-2008, 04:07 AM
Eranga
Please select the correct place before posting your question pal. Don't interfere if you question is not related to the original question. I don't think your question is not related to the Quiz Time thread. This is a general question. So I have to move your question to correct forum. Please pay your attention on this when you posting again. If you need any help regarding that(not solving questions) contact through me a PM.
• 08-11-2008, 04:17 AM
Fubarable
Your best bet is probably to condense your classes into the smallest possible code that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem, an SSCCE. Then when this is complete, post the code here and let us run it.
• 08-11-2008, 10:14 AM
willemjav
Fubarable,
the only thing I can think of is that this fact has to do with threads and local var updating.... but my program runs in a single thread! The app is quite complex and to strip of things might be very time consuming. The random method concerns two classes, one where it is declared and an other were it get called (many times). The line that prints the output is right under the rand function call, as shown above. The fact does puzzle me. I could att all four classes involved because viewing the problem rather simple?

Erenga,