Created
October 25, 2024 21:44
-
-
Save heathhenley/20ede25bd68b3e0a70dbd3869f427768 to your computer and use it in GitHub Desktop.
AceSimulation.ipynb
This file contains hidden or 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNpUfJvEk6DhpancL0mxIlz", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/heathhenley/20ede25bd68b3e0a70dbd3869f427768/acesimulation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "OKysGLXGxnwE", | |
"outputId": "c6df11fc-e946-454c-8437-676d21dde456" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Single suit case - 50000 iterations\n", | |
" Average steps: 6.990, Std. Dev: 3.742\n", | |
"Full deck case - 50000 iterations\n", | |
" Average steps: 10.574, Std. Dev: 8.244\n" | |
] | |
} | |
], | |
"source": [ | |
"from typing import List, Tuple\n", | |
"\n", | |
"import random\n", | |
"\n", | |
"\n", | |
"def strategy_random(deck: List[str], target: str = 'A') -> int:\n", | |
" \"\"\" Returns number of guesses needed to find the ace \"\"\"\n", | |
" steps = 1\n", | |
" while True:\n", | |
" flip_idx = random.randint(0, len(deck)-1)\n", | |
" if deck[flip_idx] == target:\n", | |
" break\n", | |
" deck.pop(flip_idx)\n", | |
" steps += 1\n", | |
" return steps\n", | |
"\n", | |
"\n", | |
"def stats(x_sum, x2_sum, n) -> Tuple[float, float]:\n", | |
" x_bar = x_sum / n\n", | |
" x2_bar = x2_sum / n\n", | |
" return (x_bar, (x2_bar - x_bar**2)**0.5)\n", | |
"\n", | |
"\n", | |
"def play_the_game(\n", | |
" number_of_games: int = 1, deck_size: int = 13, ace_count: int = 1\n", | |
" ) -> Tuple[float, float]:\n", | |
"\n", | |
" total_steps = 0\n", | |
" steps_sqrd_total = 0\n", | |
" for _ in range(number_of_games):\n", | |
" # make a deck and shuffle it\n", | |
" deck = list(\"A\" * ace_count + (deck_size - ace_count) * \"X\")\n", | |
" random.shuffle(deck)\n", | |
" # play the game - find the ace\n", | |
" steps = strategy_random(deck)\n", | |
" # stats\n", | |
" total_steps += steps\n", | |
" steps_sqrd_total += steps * steps\n", | |
" return stats(total_steps, steps_sqrd_total, number_of_games)\n", | |
"\n", | |
"\n", | |
"# Play the game and see what we get to check our results\n", | |
"number_of_repeats = 50_000\n", | |
"\n", | |
"# Single\n", | |
"avg, stdev = play_the_game(\n", | |
" number_of_games=number_of_repeats,\n", | |
" deck_size = 13,\n", | |
" ace_count = 1\n", | |
" )\n", | |
"print(f\"Single suit case - {number_of_repeats} iterations\")\n", | |
"print(f\" Average steps: {avg:.3f}, Std. Dev: {stdev:.3f}\")\n", | |
"\n", | |
"# Full deck\n", | |
"avg, stdev = play_the_game(\n", | |
" number_of_games=number_of_repeats,\n", | |
" deck_size = 52,\n", | |
" ace_count = 4\n", | |
" )\n", | |
"print(f\"Full deck case - {number_of_repeats} iterations\")\n", | |
"print(f\" Average steps: {avg:.3f}, Std. Dev: {stdev:.3f}\")\n", | |
"\n" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment