Ceri Westcott
Ceri Westcott

Reputation: 300

for loops displays 2 and then 3 results?

I know exactly whats happening but cannot figure it out for the life of me, i've been out of programming for a while now so excuse the ugly code..

pretty much I'm trying to do a "pick a name of the hat" concept... as I want to have random 'matches' chosen but some times I'll get displayed:

Harry v Kwok
Matthew v Lewis
James v Ceri

then others being:

Ceri v James
Kwok v Harry

  import java.util.*;
  public class hatpicking {
    public static void main(String[] args) {
   //Links one value to other--- eg. 0 = ceri, 1 = Harry    
    HashMap<Integer, String> map = new HashMap<Integer, String>();

    map.put(0, "Ceri");
    map.put(1, "Harry");
    map.put(2, "Matthew");
    map.put(3, "Lewis");
    map.put(4, "Kwok");
    map.put(5, "James");

    int HashmapValue = 6;

    //For loops that only makes 3 fixtures
    for(int i=1; i<20; i++){                                        
        //Generates 2 Random Numbers
        int first = (int) (Math.random() * HashmapValue);
        int second = (int)(Math.random()* HashmapValue );

        //Assigns the 2 numbers to the hashmap values
        String val1 = (String)map.get(first);
        String val2 = (String)map.get(second);

    if(val1 != null && val1 != val2 && val2 != null){               

        map.remove(first);
        map.remove(second);

        //prints Fixtures
        System.out.println(val2 + " v " + val1);
            }           
        }   
    }
}

Upvotes: 1

Views: 103

Answers (3)

VinyJones
VinyJones

Reputation: 1014

You should use a simple permutation over a List that represent your population: pseudo code would be

List<Player> list= {...} << put what ever represent your player

for(int i= 0 ; i < list.size ; i++) { //Random permutation
    int j = random(list.size) // return 
    list.swap(i,j);
}

List<Group> groups;
for (int i = 0 ; i < list.size - 1; i+=2){
    groups.add(new Group(list.get(i), list.get(i+1);
}

Upvotes: 0

Keppil
Keppil

Reputation: 46239

You need to get lucky that your generated values are still in the Map. You always generate a number between 0 and 5, but the map gets smaller as you remove elements. Just change the loop condition from 20 tries to keep going until map is empty:

 while (!map.isEmpty()) {
     // Generates 2 Random Numbers
     int first = (int) (Math.random() * HashmapValue);
     ...

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201507

If I understand your issue, it is because sometimes val1 == val2. You could add an else block to your if,

// != is only safe because of reference equality due to the Map(s).
if(val1 != null && val1 != val2 && val2 != null){
  map.remove(first);
  map.remove(second);

  //prints Fixtures
  System.out.println(val2 + " v " + val1);
} else {
  i--; // repeat.
}

Edit

Based on your comments, I would actually implement it with Collections.shuffle() and

String[] competitors = { "Ceri", "Harry",
    "Matthew", "Lewis", "Kwok", "James" };
Collections.shuffle(Arrays.asList(competitors));
for (int i = 0; i < competitors.length; i += 2) {
  System.out.printf("%s v %s%n", competitors[i],
      competitors[i + 1]);
}

Upvotes: 0

Related Questions