[FIXED] How can I write football match algorithm

Issue

I’m making a soccer league app. There are 18 teams in this league and each team has its own power. These powers are the average of the randomly generated number of 11 randomly generated players. I want them to play matches between them by comparing these powers. But I don’t always want the strong team to win, it just has to be highly likely. How can I do that?

public void match(Teams team1,Teams team2){
    int homeTeamGoal = 0;
    int awayTeamGoal = 0;
    if(team1.getTeamAvaragePower()> team2.getTeamAvaragePower()){


    }else if(team2.getTeamAvaragePower()>team1.getTeamAvaragePower()){

        
    }else{

        
    }
}

Solution

int totalWeight = team1.getTeamAvaragePower() + team2.getTeamAvaragePower(); 
int random = ThreadLocalRandom.current().nextInt(totalWeight);
if (random <= team1.getTeamAvaragePower()) {
    System.out.println("Team 1 Wins");
} else {
    System.out.println("Team 2 Wins");
}

Explanation: let’s say team’1 power is 30 and team’2 power is 70. That’s 30% to 70% chances. How to get a chance to compare? Let’s generate a random number from 0 to 100 — there is a 70% chance to get a number less than or equal to 70.

Case 1: random number from 0 to 100 is generated, let’s say 45.
In this way 45 > 30 and team2 wins.

Case 1: random number from 0 to 100 is generated, let’s say 21.
In this way, 21 < 30 and team1 wins.

Answered By – star67

Answer Checked By – Katrina (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published