Skip to content

Instantly share code, notes, and snippets.

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

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)
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()
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
# ==================================
def print_results(counts):
header_text = "matches occurences % of total"
for matches in sorted(set(counts)):
occurences = counts.count(matches)
print("Exact Matches:\n==============\n")
print("\n\nSame Group:\n===========\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment