Last active
June 13, 2024 19:28
-
-
Save JacobCallahan/143a904da1ff2d935b75afdb27cc4d9b to your computer and use it in GitHub Desktop.
random people selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import pathlib | |
import random | |
import sys | |
import time | |
# Define the list of names | |
names = ['stephen', 'justin', 'jake', 'david', 'cole', 'tasos', 'brian', 'danny', 'sam', 'griffin', 'mayuri', 'sudhir'] | |
# 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