Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Created September 2, 2021 15:19
Show Gist options
  • Select an option

  • Save JosephTLyons/53ff51de671409a19a8c512cd7613809 to your computer and use it in GitHub Desktop.

Select an option

Save JosephTLyons/53ff51de671409a19a8c512cd7613809 to your computer and use it in GitHub Desktop.
Simulates the trick of picking 2 random cards and then seeing if those two cards end up next to each other in a shuffled deck
import random
def main():
runs = 100_000
successes = 0
cards = list(range(13)) * 4
for _ in range(runs):
random.shuffle(cards)
random_sorted_pair = sorted(random.sample(cards, 2))
random.shuffle(cards)
if any(random_sorted_pair == sorted(adjacent_pair) for adjacent_pair in zip(cards, cards[1:])):
successes += 1
print(f"{successes} / {runs} = {successes / runs}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment