1. Senior Member
Join Date
Dec 2007
Location
Spain
Posts
1,139
Rep Power
14

## 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.

Java 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.

Java 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

2. 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.

3. 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.

4. Senior Member
Join Date
Dec 2007
Location
Spain
Posts
1,139
Rep Power
14
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,
But the fact that you acted as moderator and moved my out of the thread implies that you language should have a neutral "shape" (referring to "pal").

5. Your algorithm is wrong and only works for the value 1 to .... Try it with different values.
Nick and I both gave you working code for this.

6. Senior Member
Join Date
Dec 2007
Location
Spain
Posts
1,139
Rep Power
14
Yes norm already noticed myself and corrected the code (one is so busy with "complicated" stuff that one oversees the simple but important thinks). The code is close to the pascal one now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•