0

I've made a random wrestling match generator that I've adapted from a random phrase generator from a textbook. I'd like to know how to make it so the same name isn't done twice on the same run. Can't have The Crusher vs. The Crusher, right?

public class matchOMatic {
    public static void main (String [] args) {

    String [] wordListOne = {"The Crusher", "The Main Man", "The Macho-man, Randy Savage", "The Nature Boy, Rick Flare", "Batista", "Hollywood Hulk Hogan", "Vader", "The Undertaker", "Stone Cold Steve Austin" };
    String [] wordListThree = {"The Crusher", "The Main Man", "The Macho-man, Randy Savage", "The Nature Boy, Rick Flare", "Batista", "Hollywood Hulk Hogan", "Vader", "The Undertaker", "Stone Cold Steve Austin"};

    int oneLength = wordListOne.length;
    int threeLength = wordListThree.length;

    int rand1 = (int) (Math.random() * oneLength);
    int rand3 = (int) (Math.random() * threeLength);

    String phrase = wordListOne[rand1] + " and in the opposite corner is his opponent, " + wordListThree[rand3];

    System.out.print("In this corner we have " + phrase);
    System.out.println("!");
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Matt Lee
  • 521
  • 2
  • 5
  • 16
  • Just as an aside, you can use a single list of wrestlers instead of two, simply drawing twice from the list. –  Sep 08 '14 at 01:42

3 Answers3

2

Very simple solution for this!

Just put this after you give values to rand3 and rand2:

while(rand3 == rand1) {
    rand3 = (int) (Math.random() * threeLength);
}

This will keep choosing a new value for rand3 until the values are different!

I hope this helps. Good luck with your program :)

Alex K
  • 8,269
  • 9
  • 39
  • 57
1

The cleanest solution is to store the names in an ArrayList rather than an array of strings, shuffle the list, and iterate through in pairs to create matches. Shuffling a list of length N is O(N), and this is guaranteed to not produce duplicates across the scheduled matches.

pjs
  • 18,696
  • 4
  • 27
  • 56
  • That's a good solution too. I only wrote it this way because it was originally from a random phrase generator and each string had different words in them. There were three of them. That's why there's a 1 and a 3 but not 2. Thanks though. – Matt Lee Sep 08 '14 at 20:13
0

You could pick the next one ( or the one before) if the random turns out to be the same.

 if ( rand3 == rand1 )
   rand3 = (rand3 +1) % threeLength

Edit: As noted below this creates bias which is not good.

other possible solution is not to include the first randomly picked index in the next random roll

// -1 because we are not including the same pick
int rand3 = (int) (Math.random() * ( threeLength-1) );

// fix the index because we haven't actually removed it from the array
if (rand3 >= rand1)
    rand3 = (rand3 +1) % threeLength
Zentdayn
  • 62
  • 2
  • 9
  • This will create a bias for matches between successive wrestlers. –  Sep 08 '14 at 01:40
  • Indeed it does, proposed a new solution which i think it avoids such problem. – Zentdayn Sep 08 '14 at 13:43
  • 1
    Once you fix the range bound error (the rand3++ needs to wrap-around), this still creates a bias. While the second roll makes a self-match impossible, it creates two ways to pick the first guy on the list. Consider testing your approaches by running them 10,000 times and counting the matchups in a matrix. That will surface any biases. –  Sep 08 '14 at 16:30
  • Silly me on on the rand3. I'll count the matchups as i get home, i did think before hand if it would be biased again, i though that i addressed that issue by making the random roll to -1 the limit of the first one ( like if i removed the first random item then made a random choice of the ones that where left) the increment on rand3 is just to act like if the previous item wasn't there because im randomly picking off smaller set. – Zentdayn Sep 08 '14 at 18:25
  • Just did it ideone.com/aGElQs. I looks nicely distributed, ie nothing picked more than it should. Did you notice this change rand3 = (int) (Math.random() * **( threeLength-1)** ); – Zentdayn Sep 08 '14 at 18:39
  • Create a *matrix* that counts the ordered pairs . I believe you will see that the first guy shows in matches more often. The basic reason is that when you pick the first guy, you tend to leave him alone (since he is early in the list), but when you pick the last guy, you tend to replace him with the first guy instead. –  Sep 08 '14 at 18:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60843/discussion-between-willie-wheeler-and-zentdayn). –  Sep 08 '14 at 18:55