Skip to content

Instantly share code, notes, and snippets.

@Blacklock
Created February 20, 2019 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blacklock/0057022786bf86cbc9bb087661f3742d to your computer and use it in GitHub Desktop.
Save Blacklock/0057022786bf86cbc9bb087661f3742d to your computer and use it in GitHub Desktop.
Generates random setups for the Primrose mafia game.
import random
def weighted_choice(weights):
rnd = random.random() * sum(weights.values())
for i, w in enumerate(weights.values()):
rnd -= w
if rnd < 0:
return list(weights)[i]
rounds = 10000000 # How many times we run the simulation
letters_amount = 9 # How many letters are generated
letters = { # Not the fastest way of doing this, but who cares
'T': 40,
'W': 10,
'C': 15,
'D': 10,
'V': 10,
'M': 10,
'B': 5
}
setups = dict() # Logs the amount of each possible setup generated
for _ in range(rounds):
setup = list()
for _ in range(letters_amount):
choice = weighted_choice(letters)
setup += choice
setup.sort(key=lambda l: list(letters).index(l))
setup = ''.join(setup)
setups[setup] = setups.get(setup, 0) + 1
# NB! Make sure you have created a results.txt file beforehand!
with open('results.txt', 'w') as f:
results = '\n'.join([f'{setup}\t{setups[setup]}' for setup in sorted(setups, key=setups.get, reverse=True)])
f.write(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment