7 Replies Latest reply: Oct 12, 2010 4:24 PM by YoungWinston RSS

    Another Random Numbers Problem

    803051
      I say "Another" because there's an existing random numbers thread on the first page of this forum, but it doesn't answer my problem.

      I'm generating names for an array of objects. I have an array of words (Strings) to choose from. I'm using Math.random()*151 to select the two word array indices. I want to check to make sure the two selected integers are not the same before assigning the name to the object. Then, I want to check to ensure that no two objects have the same name. Simple, right? Here's the code:
          for(int i=0;i<numStars;i++){
              ok=true;
              do{
              int part1=(int)(Math.random()*151);//first index
              int part2=(int)(Math.random()*151);//second index
              if (part1==part2){//make sure they're not the same number
                  ok=false;
                  System.out.println(part1+" "+part2);
              }else{
                  stars.name=new String(starName[part1]+" "+starName[part2]);
      for(int j=0;j<i;j++){
      if(stars[i].name.equals(stars[j].name)) {//make sure no two stars have the same name
      ok=false;
      System.out.println(stars[i].name+" "+stars[j].name);
      }
      }
      }
      }while( ok==false);
      )//end of for loop


      The problem is, if integers part1 and part2 are equal in one pass through the do loop, they remain identical in each subsequent pass. The number doesn't stay the same, but whatever part1 is, part2 is always the same thing. This is only true after matching up one time. It's possible for it to run without ever hitting a duplicate.

      I changed it to ensure that part1 and part2 weren't covering the same range of numbers, and the same error started happening in the lower for loop that compares one object's name to another. This second-level error doesn't occur when part1 and part2 both cover the full range.

      Edited by: 800048 on Oct 11, 2010 3:17 PM

      Edited by: 800048 on Oct 11, 2010 3:25 PM
        • 1. Re: Another Random Numbers Problem
          796440
          800048 wrote:
          The problem is, if integers part1 and part2 are equal in one pass through the do loop, they remain identical in each subsequent pass.
          I don't believe that's possible with the code you've posted.

          What will happen, however, since you never set ok=true once part1 and part2 become unequal, is that if they're equal in the first pass through the do/while loop, you will never break out of that loop.

          Also, it's pointless and poor form to use == or != with true or false
          // bad
          x == true
          x != false
          
          // good
          x
          
          
          // bad
          x != true
          x == false
          
          // good
          !x
          • 2. Re: Another Random Numbers Problem
            803051
            Argh, I am a moron.

            Although the situation presented itself as I described, this was what was happening. I assume it was running the loop so often and so fast once it got locked in that it started getting a lot of hits by sheer numbers, which looked like a constant behavior to me. Which tells me it runs really fast, which is nice to know.

            Thanks, jverd.
            • 3. Re: Another Random Numbers Problem
              YoungWinston
              800048 wrote:
              I'm generating names for an array of objects. I have an array of words (Strings) to choose from. I'm using Math.random()*151 to select the two word array indices. I want to check to make sure the two selected integers are not the same before assigning the name to the object. Then, I want to check to ensure that no two objects have the same name. Simple, right?
              I suspect you're overthinking this (don't worry, we've all done it; me included :-) ).

              First: By using arrays you're cutting yourself off from the rich set of functions available to you with Java's collection framework.

              Second: If you have a deck of cards and you wanted to hand someone 10 random unique cards, how would you do it? Hint: have a look at Collections.shuffle(List)
              (http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle%28java.util.List%29).

              Winston

              PS: Anyone care to tell me how you create "named" links? the 'Link' button doesn't seem to work.
              • 4. Re: Another Random Numbers Problem
                JoachimSauer
                YoungWinston wrote:
                PS: Anyone care to tell me how you create "named" links? the 'Link' button doesn't seem to work.
                [url=http://www.google.com/]Example[/url]
                • 5. Re: Another Random Numbers Problem
                  YoungWinston
                  Joachim Sauer wrote:
                  YoungWinston wrote:
                  PS: Anyone care to tell me how you create "named" links? the 'Link' button doesn't seem to work.
                  [url=http://www.google.com/]Example[/url]
                  Ah. Thanks Joachim.

                  Winston
                  • 6. Re: Another Random Numbers Problem
                    803051
                    I'm using a arrays here because the objects I'm dealing with are fixed in number and name. Once they're created, they're not getting added to or destroyed or renamed. So I went with the data structure I find easiest to deal with.

                    Next come the objects that can be changed, created, and/or destroyed, and I'll be getting into the more flexible methods. That is, after I perfect writing relevant text about the stars out beside them. But that's another topic.
                    • 7. Re: Another Random Numbers Problem
                      YoungWinston
                      800048 wrote:
                      I'm using a arrays here because the objects I'm dealing with are fixed in number and name. Once they're created, they're not getting added to or destroyed or renamed. So I went with the data structure I find easiest to deal with.
                      It still doesn't stop you using the shuffle method:
                      1. Set up your array of names.
                      2. Convert it to a List (see Arrays.asList()).
                      3. Shuffle it.
                      4. Pull the first n names from the resulting List and stick them in your "randomized" array.

                      Winston