lundi 21 décembre 2020

Running the same algorithm with the same input and getting different outputs

I m trying to make an application that is simulating an athletic event (in this case, Champions League). So i am asking for input the names of 16 teams and i store them in an ArrayList. Then i have to make the draw and split 16 teams in 4 groups of 4 teams. I use math.random to generate a number between 1-4 . Then i have the following statement :

switch(random){
            case 1:
                if(GroupPhase.group1.isFull == false)
                {
                   GroupPhase.group1.addToGroup(teams.get(i));
                   if(GroupPhase.group1.getSize()== 4) 
                   {
                       GroupPhase.group1.isFull = true;
                   }
                   random = (int)(Math.random()*4) +1;
                }
                else
                {
                   fillToEmptyGroup(teams.get(i));
                   random = (int)(Math.random()*4) +1;
                }
                break;
            case 2:
                if(GroupPhase.group2.isFull == false)
                {
                    GroupPhase.group2.addToGroup(teams.get(i));
                    if(GroupPhase.group2.getSize()== 4)
                    {
                         GroupPhase.group2.isFull = true;
                    }
                    random = (int)(Math.random()*4) +1;
                }
                else
                {
                   fillToEmptyGroup(teams.get(i));
                   random = (int)(Math.random()*4) +1;
                }
                break;
            case 3:
                if(GroupPhase.group3.isFull == false)
                {
                    GroupPhase.group3.addToGroup(teams.get(i));
                    if(GroupPhase.group3.getSize()== 4) 
                    {
                        GroupPhase.group3.isFull = true;
                    }
                    random = (int)(Math.random()*4) +1;
                }
                else
                {
                  fillToEmptyGroup(teams.get(i));
                  random = (int)(Math.random()*4) +1;
                }
                break;
            case 4 :
                if(GroupPhase.group4.isFull == false)
                {
                    GroupPhase.group4.addToGroup(teams.get(i));
                    if(GroupPhase.group4.getSize()== 4)
                    {
                        GroupPhase.group4.isFull = true;
                    }
                    random = (int)(Math.random()*4) +1;
                }
                else
                {
                    fillToEmptyGroup(teams.get(i));
                    random = (int)(Math.random()*4) +1;
                }
                break;
            }
        }

To explain it further , i generate a random number between 1-4. If the ArrayList size is less than 4 i put the team in the group that matches the generated number.If it is full i call an other function that checks which group`s ArrayList size is less than 4 and adds the team in it.

public static void fillToEmptyGroup(Teams team)
    {
        if(!GroupPhase.group1.isFull)
        {
            GroupPhase.group1.addToGroup(team);
        }
        if(!GroupPhase.group2.isFull)
        {
            GroupPhase.group2.addToGroup(team);
        }
        if(!GroupPhase.group3.isFull)
        {
            GroupPhase.group3.addToGroup(team);
        }
        if(!GroupPhase.group4.isFull)
        {
            GroupPhase.group4.addToGroup(team);
        }
        
    }

I think the logic is right. But when i run it, i have different outputs with the same inputs. Sometimes i get a group with 5+ team and the same team in 2 groups the same time! I really cant understand whats happening here. Tried the debugger and i figured out that the isFull variable never gets updated. I dont know how ! Its clear in the code that i set it true in the case the size of the ArrayList is 4. Please help me

Aucun commentaire:

Enregistrer un commentaire