samedi 12 décembre 2015

How to Weight Random Numbers in Python

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?




Aucun commentaire:

Enregistrer un commentaire