Results 1 to 6 of 6
- 08-08-2008, 10:10 PM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 573
- Rep Power
- 6
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.
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 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; }
But when calling this method from a different class e.g.Java Code:public static void main (String args[]) { new fmEdit(); for (int i = 0; i < 200; i++) random(1, 10); }
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 #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
-
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 #4
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 573
- Rep Power
- 6
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,
Your answer as the thread starter is probably correct.
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").
- 08-11-2008, 01:52 PM #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.
- 08-11-2008, 09:31 PM #6
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 573
- Rep Power
- 6
Similar Threads
-
Strange behaviour in swing
By cbalu in forum AWT / SwingReplies: 1Last Post: 05-23-2008, 09:23 PM -
How to handle each display/shell dialog box (not knowed) and choose default answer
By lenar in forum SWT / JFaceReplies: 0Last Post: 04-04-2008, 12:15 AM -
What is the answer yo my problem with this code
By masaka in forum New To JavaReplies: 4Last Post: 03-26-2008, 06:33 AM -
Need help on a strange file access prob
By squad in forum Advanced JavaReplies: 0Last Post: 03-21-2008, 10:55 AM -
System.in.read(); strange error!
By kantze in forum New To JavaReplies: 2Last Post: 03-19-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks