{ "cells": [ { "cell_type": "markdown", "id": "e29535e2-230e-4ba8-a9c6-d30dce1a6011", "metadata": {}, "source": [ "# Chapter 9.3 - Simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "d3528751-32b4-4f83-b9a1-52cea6766425", "metadata": {}, "outputs": [], "source": [ "from datascience import *\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "b63d1bde-8a1f-403f-a7ff-16864ced198e", "metadata": {}, "source": [ "## [Farkle] (https://www.wikihow.com/Play-Farkle)\n", "Let's simulate how often some of the higher rolling scores occur on the roll of all 6 dice" ] }, { "cell_type": "code", "execution_count": null, "id": "30a0cf52-909f-424e-ad51-34fdc2562fd7", "metadata": {}, "outputs": [], "source": [ "def farkle_roll():\n", " rolls = make_array()\n", " for _ in range(6):\n", " rolls = np.append(rolls, np.random.randint(1,7))\n", " return np.sort(rolls)\n", "\n", "farkle_roll()" ] }, { "cell_type": "code", "execution_count": null, "id": "3e7665c2-3e2c-47fa-98e8-70aade69a8c2", "metadata": {}, "outputs": [], "source": [ "def farkle_score(roll):\n", " if roll[0] == roll[5]:\n", " return \"Sextuplet\"\n", " elif roll[0] == roll[2] and roll[3] == roll[5]:\n", " return \"2 Triplets\"\n", " elif roll[0] == roll[4] or roll[1] == roll[5]:\n", " return \"Quintuplet\"\n", " elif (roll[0] == roll[3] and roll[4] == roll[5]) or (roll[0] == roll[1] and roll[2] == roll[5]):\n", " return \"Quadruplet + Pair\"\n", " elif roll[0] == roll[3] or roll[1] == roll[4] or roll[2] == roll[5]:\n", " return \"Quadruplet\"\n", " else:\n", " return \"Unknown\"\n", "\n", "roll = farkle_roll()\n", "farkle_score(roll)" ] }, { "cell_type": "code", "execution_count": null, "id": "5d011fc8-c18f-4936-ad58-1c89c10610f3", "metadata": {}, "outputs": [], "source": [ "trials = make_array()\n", "for _ in range(10000):\n", " trials = np.append(trials, farkle_score(farkle_roll()))\n", "print(\"done\")\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "ed40724c-d250-4328-80ea-7dcf1db1d134", "metadata": {}, "outputs": [], "source": [ "farkle_simulation = Table().with_column(\"Result\", trials)\n", "farkle_simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "fa057b19-1795-402a-a4bd-876560a2d5bc", "metadata": {}, "outputs": [], "source": [ "farkle_simulation = farkle_simulation.group(\"Result\")\n", "farkle_simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "cc99ef03-c59c-4e87-b314-da9b5fc26402", "metadata": {}, "outputs": [], "source": [ "farkle_simulation = farkle_simulation.sort(\"count\")\n", "farkle_simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "231abdfc-96c0-4212-aa98-ba87421b67c4", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "farkle_simulation.barh(\"Result\")" ] }, { "cell_type": "code", "execution_count": null, "id": "de66026f-d464-45c9-a9aa-886fc12c1aa5", "metadata": {}, "outputs": [], "source": [ "farkle_simulation = farkle_simulation.with_column(\"Percent\", farkle_simulation.column(\"count\") / sum(farkle_simulation.column(\"count\")))\n", "farkle_simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "3772a88d-4fd6-45a9-b2a8-a4d82bc48776", "metadata": {}, "outputs": [], "source": [ "farkle_simulation.set_format(\"Percent\", PercentFormatter)\n", "farkle_simulation = farkle_simulation.drop(\"count\")\n", "farkle_simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "9043bbaf-11f4-44b6-adff-e3e0a3d4440a", "metadata": {}, "outputs": [], "source": [ "farkle_simulation.barh(\"Result\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }