Python Forum
Randomizer script to provide options based on user and priority
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randomizer script to provide options based on user and priority
#2
Something like this?
import random
import collections
 
ALL_GAMES = [
    "Candyland",
    "COD",
    "Connect 4",
    "Life",
    "Monopoly",
    "Risk",
    "Skippo",
    "Uno",
    "Yahtzee"
]
 
class GamerMeta(type):
    """So we can do Gamer[player_name]"""
    def __getitem__(cls, name):
        return cls.Gamers[name]
 
class Gamer(metaclass=GamerMeta):
    Gamers = {}  # Dictionary of all players choices
 
    def __init__(self, name, games):
        """Create Gamer object.  Verify games choices"""
        for game in games:
            if game not in ALL_GAMES:
                raise ValueError(f"{game} is not in our collection")
        self.name = name
        self.games = games
        self.Gamers[name] = self  # Add gamer to Gamers list
 
    def votes(self):
        """Get weighted game selections for this Gamer"""
        if self.games == ALL_GAMES:
            return ALL_GAMES  # Game catalog is not weighted
 
        # Weight gamer's choices.  First choice gets 5 votes, second 4 votes and so on.
        vote = []
        for game, weight in zip(self.games, [5, 4, 3, 2, 1]):
            vote += [game] * weight
        return vote

    @classmethod
    def cast_ballots(cls, gamers):
        """Collect votes from gamers"""
        ballots = []
        for gamer in gamers:
            ballots += cls.Gamers[gamer].votes()
        return ballots
 
    def __repr__(self):
        """Get string with info about this gamer"""
        return f"{self.name} games = {self.games}"

def random_choice(*gamers):
    """Randomly choose from "votes" for all of tonights gamers"""
    return random.choice(Gamer.cast_ballots(gamers))
 
def weighted_choice(*gamers):
    """
    Select favorite game as voted on by gamers.  If multiple games tie
    as the favorite, randomly select from those games
    """
    ballots = collections.Counter(Gamer.cast_ballots(gamers)).most_common()
    return random.choice([game[0] for game in ballots if game[1] == ballots[0][1]])
 
# Add gamers to the "database"
Gamer("Catalog", ALL_GAMES)
Gamer("Dad", ("Uno", "Monopoly", "Candyland")),
Gamer("Mom", ("Life", "Uno", "Connect 4")),
Gamer("Thing 1", ("COD", "Monopoly", "Skippo", "Risk", "Uno")),
Gamer("Thing 2", ("Yahtzee", "Skippo", "Uno", "Life"))
 
# Try some random combinations to see how it works.
print("Taking a chance")
print("Tonight we play", random_choice("Catalog"))
print("Tonight we play", random_choice("Thing 1", "Thing 2"))
print("Tonight we play", random_choice("Mom", "Dad"))
print("Tonight we play", random_choice("Mom", "Dad", "Thing 1", "Thing 2"))
print("Tonight we play", random_choice("Mom", "Thing 1", "Catalog"))
 
# Try some weighted combinations to see how it works.
print("\nStrict democracy in action")
print("Tonight we play", weighted_choice("Catalog"))
print("Tonight we play", weighted_choice("Thing 1", "Thing 2"))
print("Tonight we play", weighted_choice("Mom", "Dad"))
print("Tonight we play", random_choice("Mom", "Dad", "Thing 1", "Thing 2"))
print("Tonight we play", weighted_choice("Mom", "Thing 1", "Catalog"))
This program can randomly selects a game from "weighted" player selections or it can select the game with the most votes. Weighting and voting are done by giving a player's first choice 5 votes, their second choice 4 votes and so on. Randomly selecting a game from the weighted has the highest probability of picking the game with the most number of votes, but being random can also pick the game with the least.
Reply


Messages In This Thread
RE: Randomizer script to provide options based on user and priority - by deanhystad - Mar-10-2022, 09:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  output provide the filename along with the input file processed. arjunaram 1 982 Apr-13-2023, 08:15 PM
Last Post: menator01
  Priority Queue with update functionality PythonNewbee 4 2,081 Dec-29-2022, 12:13 PM
Last Post: Yoriz
  Performance options for sys.stdout.writelines dgrunwal 11 3,278 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Exit function from nested function based on user input Turtle 5 3,011 Oct-10-2021, 12:55 AM
Last Post: Turtle
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,868 May-18-2021, 06:42 PM
Last Post: Skaperen
  Creating a calculation based on user entry blakefindlay 2 2,066 Jan-26-2021, 06:21 PM
Last Post: blakefindlay
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,581 Jan-16-2021, 10:20 PM
Last Post: Miletkir
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,996 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Can argparse support undocumented options? pjfarley3 3 2,304 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  Help with options raiden 1 1,965 Aug-30-2019, 12:57 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020