\n", "\n", "## Problem Statement\n", "*Is the probability of coin flip landing on heads actually 50%?*\n", "\n", "## Solution 1\n", "Below is a simple python script that randomly flips a coin (0 = heads, 1 = tails) for the number of times the user specifies. Go ahead, try it yourself. Are they really 50/50? " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solution 1: \n", "\n", "How many times do you want to flip the coin? 50\n", "\n", "38.0% (19) were Heads \n", "62.0% (31) were Tails\n" ] } ], "source": [ "#Heads Tails Prob Calculator by Techman\n", "import random\n", "total = int(input(\"Solution 1: \\n\\nHow many times do you want to flip the coin? \"))\n", "heads, tails = 0, 0\n", "for i in range(total):\n", "\tif random.randint(0,1) == 0:\n", "\t\theads += 1\n", "\telse:\n", "\t\ttails += 1\n", "print(\"\\n{headsp}% ({heads}) were Heads \\n{tailsp}% ({tails}) were Tails\".format(heads=heads, tails=tails, headsp=round(heads*100/total, 2), tailsp=round(tails*100/total, 2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "\n", "## Solution 2\n", "\n", "This solution asks how many times you want to flip the coin per set, then prints how many sets of coin flips and how long it takes to achieve a result that is 50/50. Check it out below." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solution 2: \n", "\n", "How many times do you want to flip the coin? 1000\n", "\n", "49.1% (491) were Heads \n", "50.9% (509) were Tails\n", "\n", "48.7% (487) were Heads \n", "51.3% (513) were Tails\n", "\n", "50.5% (505) were Heads \n", "49.5% (495) were Tails\n", "\n", "51.5% (515) were Heads \n", "48.5% (485) were Tails\n", "\n", "49.8% (498) were Heads \n", "50.2% (502) were Tails\n", "\n", "49.0% (490) were Heads \n", "51.0% (510) were Tails\n", "\n", "48.4% (484) were Heads \n", "51.6% (516) were Tails\n", "\n", "50.8% (508) were Heads \n", "49.2% (492) were Tails\n", "\n", "51.2% (512) were Heads \n", "48.8% (488) were Tails\n", "\n", "49.6% (496) were Heads \n", "50.4% (504) were Tails\n", "\n", "47.9% (479) were Heads \n", "52.1% (521) were Tails\n", "\n", "50.8% (508) were Heads \n", "49.2% (492) were Tails\n", "\n", "48.4% (484) were Heads \n", "51.6% (516) were Tails\n", "\n", "54.5% (545) were Heads \n", "45.5% (455) were Tails\n", "\n", "51.2% (512) were Heads \n", "48.8% (488) were Tails\n", "\n", "47.0% (470) were Heads \n", "53.0% (530) were Tails\n", "\n", "49.4% (494) were Heads \n", "50.6% (506) were Tails\n", "\n", "49.6% (496) were Heads \n", "50.4% (504) were Tails\n", "\n", "47.3% (473) were Heads \n", "52.7% (527) were Tails\n", "\n", "50.4% (504) were Heads \n", "49.6% (496) were Tails\n", "\n", "51.9% (519) were Heads \n", "48.1% (481) were Tails\n", "\n", "50.9% (509) were Heads \n", "49.1% (491) were Tails\n", "\n", "52.0% (520) were Heads \n", "48.0% (480) were Tails\n", "\n", "51.1% (511) were Heads \n", "48.9% (489) were Tails\n", "\n", "48.1% (481) were Heads \n", "51.9% (519) were Tails\n", "\n", "49.3% (493) were Heads \n", "50.7% (507) were Tails\n", "\n", "50.7% (507) were Heads \n", "49.3% (493) were Tails\n", "\n", "48.9% (489) were Heads \n", "51.1% (511) were Tails\n", "\n", "50.8% (508) were Heads \n", "49.2% (492) were Tails\n", "\n", "48.1% (481) were Heads \n", "51.9% (519) were Tails\n", "\n", "51.4% (514) were Heads \n", "48.6% (486) were Tails\n", "\n", "46.6% (466) were Heads \n", "53.4% (534) were Tails\n", "\n", "49.9% (499) were Heads \n", "50.1% (501) were Tails\n", "\n", "53.1% (531) were Heads \n", "46.9% (469) were Tails\n", "\n", "50.9% (509) were Heads \n", "49.1% (491) were Tails\n", "\n", "51.9% (519) were Heads \n", "48.1% (481) were Tails\n", "\n", "50.8% (508) were Heads \n", "49.2% (492) were Tails\n", "\n", "47.6% (476) were Heads \n", "52.4% (524) were Tails\n", "\n", "47.6% (476) were Heads \n", "52.4% (524) were Tails\n", "\n", "47.0% (470) were Heads \n", "53.0% (530) were Tails\n", "\n", "48.7% (487) were Heads \n", "51.3% (513) were Tails\n", "\n", "50.7% (507) were Heads \n", "49.3% (493) were Tails\n", "\n", "50.6% (506) were Heads \n", "49.4% (494) were Tails\n", "\n", "49.9% (499) were Heads \n", "50.1% (501) were Tails\n", "\n", "51.9% (519) were Heads \n", "48.1% (481) were Tails\n", "\n", "49.3% (493) were Heads \n", "50.7% (507) were Tails\n", "\n", "51.7% (517) were Heads \n", "48.3% (483) were Tails\n", "\n", "49.0% (490) were Heads \n", "51.0% (510) were Tails\n", "\n", "50.0% (500) were Heads \n", "50.0% (500) were Tails\n", "\n", "49 attempts in 0.086 seconds\n" ] } ], "source": [ "#Heads Tails Prob Calculator by Techman\n", "import random, time\n", "attempts = 0\n", "total = int(input(\"Solution 2: \\n\\nHow many times do you want to flip the coin? \"))\n", "if total % 2 != 0:\n", " raise ValueError('Cannot use odd number')\n", "start_time = time.perf_counter()\n", "while True:\n", " attempts += 1\n", " heads, tails = 0, 0\n", " for i in range(total):\n", "\t if random.randint(0,1) == 0:\n", "\t\t heads += 1\n", "\t else:\n", "\t\t tails += 1\n", " print(\"\\n{headsp}% ({heads}) were Heads \\n{tailsp}% ({tails}) were Tails\".format(heads=heads, tails=tails, headsp=round(heads*100/total, 2), tailsp=round(tails*100/total, 2)))\n", " if heads == total / 2:\n", " break\n", "print(\"\\n{attempts} attempts in {time} seconds\".format(attempts=attempts, time = round(time.perf_counter()-start_time, 3)))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.6+" } }, "nbformat": 4, "nbformat_minor": 2 }