Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I write football match algorithm

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading