Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Thumbs up 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    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").

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    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. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    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.

Similar Threads

  1. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 09:23 PM
  2. Replies: 0
    Last Post: 04-04-2008, 12:15 AM
  3. What is the answer yo my problem with this code
    By masaka in forum New To Java
    Replies: 4
    Last Post: 03-26-2008, 06:33 AM
  4. Need help on a strange file access prob
    By squad in forum Advanced Java
    Replies: 0
    Last Post: 03-21-2008, 10:55 AM
  5. System.in.read(); strange error!
    By kantze in forum New To Java
    Replies: 2
    Last Post: 03-19-2008, 01:44 PM

Posting Permissions

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