1

I came across a script that provides me with a random dice roll between 1 and 6.

import random
min = 1
max = 6

roll_again = "yes"

while roll_again == "yes" or roll_again == "y":
    print random.randint(min, max)
    print random.randint(min, max)

    roll_again = raw_input("Roll the dices again?")

So, I decided to take this script and create a script that uses the random dice roll values and use those as scores in a soccer game.

while roll_again == "yes" or roll_again == "y":
    team1_score = random.randint(min, max)
    team2_score = random.randint(min, max)

    roll_again = raw_input("Roll the dices again?")

Now, I have around 20 soccer teams that play in the league. I created a spreadsheet and built a schedule for this league. I read in the 2 teams that are playing each game and roll the dice in the script above and that gives me the score for that game (i.e. team1_score = 5, team2_score = 3)

Now, just like in real life, some of those teams are stronger than others, so I want to write a script that weighs teams stronger than others, and thereby giving it a better chance at winning (higher score than the weaker team). So, if I had a list like this:

Team Weight

team1 60

team2 20

team3 8

team3 8

team5 4

How would I pass that into random.randint(min,max) so that it gives the better probability that the stronger team would get the higher score than the weaker team?

Mike
  • 4,099
  • 17
  • 61
  • 83
  • Thanks for the suggestion. I didn't use the suggested page, but while I reviewed it, I found another solution in the answered solution in this question: http://stackoverflow.com/questions/14992521/python-weighted-random – Mike Dec 15 '15 at 22:04

1 Answers1

2

You can first find a relation between the weight of your teams, then divide the random range by this relations, for example here the weight of team1 is 7.5 time greater than team3 so When you want to choose a score between this two team based on this relation you can choose the score for team1 between max - ((max-min)/7.5) and for team3 between max and min.

Mazdak
  • 105,000
  • 18
  • 159
  • 188