Skip to content

Instantly share code, notes, and snippets.

@cole-wilson
Created December 1, 2023 06:03
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 cole-wilson/7b39cdd089e9d66c7af9cc917d16ecf7 to your computer and use it in GitHub Desktop.
Save cole-wilson/7b39cdd089e9d66c7af9cc917d16ecf7 to your computer and use it in GitHub Desktop.
AP Stats SImulation

The following Python code simulates the chances of a random seating assignment based on students selected their seats from randomly drawn cards. What is the chance that one or more student will have the eact same seat as before? The same group? Try it out online at https://www.online-python.com/jYsmy9nK7L.

import random
# Constants we will use for the whole simulation
num_students = int(input("number of students?\n> ")) # convert to integer using int(), will break if you type something that isn't an integer
trials = int(input("# of trials to run?\n> ").replace(",","")) # remove commas so you can types things like 1,000
num_cards = 32
group_size = 4
students = list(range(1, num_students +1)) # range() is non-inclusive so you have to add 1
def shuffle_and_pick_cards():
global students # this lets us use the students variable inside a function
cards = list(range(1, num_cards +1))
random.shuffle(cards) # because the deck is shuffled (I hope)
random.shuffle(students) # because the students always come in in a (somewhat) random order
output = {} # a key:value mapping of student:card.
for student in students:
card_picked = random.choice(cards)
cards.remove(card_picked)
output[student] = card_picked
return output # send output back to trial code
def run_trial():
day1 = shuffle_and_pick_cards()
day2 = shuffle_and_pick_cards()
same_count = 0
group_count = 0
for student in sorted(students):
card1 = day1[student] # reference the key:value mappings from earlier
card2 = day2[student]
if card1 == card2:
same_count += 1
if (card1-1)//group_size == (card2-1)//group_size: # integer division: remainders are ignored 0-based so we subtract 1
group_count += 1
return same_count, group_count
counts = []
group_counts = []
print() # empty line to seperate from earlier inputs
for i in range(trials):
matches, group_matches = run_trial()
counts.append(matches)
group_counts.append(group_matches)
print("progress:" + str(i).rjust(len(str(trials))) + "/" + str(trials) + " (" + str(round(100 * 100*(i/trials))/100) + "%)", end="\r")
# ==================================
# rest of code just displays results
# ==================================
print('\n\n')
def print_results(counts):
header_text = "matches occurences % of total"
print(header_text)
print('-'*len(header_text))
for matches in sorted(set(counts)):
occurences = counts.count(matches)
print(
matches,
occurences,
"{:.3f}".format(round(100*(100*occurences/trials))/100).rjust(6),
sep="\t\t"
)
print('-'*len(header_text))
print(f"total:\t\t{trials}\t\t100.0")
print("Exact Matches:\n==============\n")
print_results(counts)
print("\n\nSame Group:\n===========\n")
print_results(group_counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment