Skip to content

Instantly share code, notes, and snippets.

@JacobCallahan
Last active March 14, 2024 13:20
Show Gist options
  • Save JacobCallahan/143a904da1ff2d935b75afdb27cc4d9b to your computer and use it in GitHub Desktop.
Save JacobCallahan/143a904da1ff2d935b75afdb27cc4d9b to your computer and use it in GitHub Desktop.
random people selector
import math
import pathlib
import random
import sys
import time
# Define the list of names
names = ['tasos', 'brian', 'mayuri', 'sudhir', 'sam', 'stephen', 'justin', 'cole', 'griffin', 'jake', 'david', 'danny']
# Create a list of weights that decreases logarithmically as you move through the list
weights = [math.log(index) for index in range(len(names), 0, -1)]
# Use random.choices() to select a name from the list
chosen_name = random.choices(names, weights, k=1)[0]
# # update this script to put the chosen name at the back of the list
names.remove(chosen_name)
names.append(chosen_name)
# Simulate spinning wheel
sleep_time = 0.05
for _ in range(5):
for char in names:
sys.stdout.write('\r' + char + ' ')
time.sleep(sleep_time)
sys.stdout.flush()
sleep_time += 0.1
print(f"\nCongratulations: {chosen_name}!")
file_contents = pathlib.Path(__file__).read_text().splitlines()
for i, line in enumerate(file_contents):
if line.startswith("names = "):
file_contents[i] = f"names = {names}"
break
pathlib.Path(__file__).write_text("\n".join(file_contents))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment