7
\$\begingroup\$

Anyways, I am running a pool where 64 entrants will receive a random team from a field of 64 teams. I'd like for it to be randomized to a decent extent, and although I understand the limitations of RNGs, I think this should be good enough. See my code below - are there any issues with this? Could this be done in an easier way? Or alternatively, is this better asked in Cross Validated?

import random

regions = ["East", "West", "Midwest", "South"]
playerlist = []
teamlist = []

# this will be an actual list of player names when completed 
for i in range(0, 64):
    playerlist.append("Player " + str(i))

for i in range(0, 7):
    random.shuffle(playerlist)

for i in range(0, 4):
    for j in range(1, 17):
        teamlist.append(playerlist[i*16+j-1] + ": " + regions[i] + " " + str(j))

#  edited: this is unnecessary
#for i in range(0, 7):
#    random.shuffle(teamlist)

print(teamlist)
\$\endgroup\$
4
\$\begingroup\$

Randomly shuffling a list seven times is not any better than doing it once. So random.shuffle(playerlist) is enough.

In Python you almost never want to iterate over the indices of a list, rather iterate over the list itself. Here you could do:

import random
import itertools

regions = "East", "West", "Midwest", "South"
players = ["Player {}".format(i) for i in range(64)]
teams = range(1, len(players) / len(regions) + 1)
random.shuffle(players)

teamlist = ["{}: {} {}".format(player, *team) for player, team in 
            zip(players, itertools.product(regions, teams))]

print(teamlist)

Here I used str.format to make formatting the different strings easier and itertools.product to construct a list (actually an iterable) of region, team:

>>> list(itertools.product(regions, teams))
[('East', 1), ('East', 2), ('East', 3), ('East', 4), ('East', 5), ('East', 6), ('East', 7), ('East', 8), ('East', 9), ('East', 10), ('East', 11), ('East', 12), ('East', 13), ('East', 14), ('East', 15), ('East', 16), ('West', 1), ('West', 2), ('West', 3), ('West', 4), ('West', 5), ('West', 6), ('West', 7), ('West', 8), ('West', 9), ('West', 10), ('West', 11), ('West', 12), ('West', 13), ('West', 14), ('West', 15), ('West', 16), ('Midwest', 1), ('Midwest', 2), ('Midwest', 3), ('Midwest', 4), ('Midwest', 5), ('Midwest', 6), ('Midwest', 7), ('Midwest', 8), ('Midwest', 9), ('Midwest', 10), ('Midwest', 11), ('Midwest', 12), ('Midwest', 13), ('Midwest', 14), ('Midwest', 15), ('Midwest', 16), ('South', 1), ('South', 2), ('South', 3), ('South', 4), ('South', 5), ('South', 6), ('South', 7), ('South', 8), ('South', 9), ('South', 10), ('South', 11), ('South', 12), ('South', 13), ('South', 14), ('South', 15), ('South', 16)]

Note that 0 is the default start argument for a range.

If your script becomes longer, you should probably add a if __name__ == "__main__": guard to allow importing your code from other scripts.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.