Created
March 23, 2020 18:48
-
-
Save jaimezorno/c49c56d5b80ee8427a72b73eecd9b869 to your computer and use it in GitHub Desktop.
Coin toss simulation for Medium article
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Coin tossing simulation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Definition of simulation function" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 14, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def coin_toss_simul(n_simulations):\n", | |
| " heads = 0\n", | |
| " for i in range(n_simulations):\n", | |
| " s = np.random.uniform(0,1)\n", | |
| " if s >= 0.5:\n", | |
| " heads = heads + 1\n", | |
| " \n", | |
| " print(\"There were {} heads out of {} simulations\".format(heads,n_simulations))\n", | |
| " print(\"You won {}% of the times\".format(heads/n_simulations*100))\n", | |
| " print(\"\\n\") " | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Simulation" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "There were 2 heads out of 2 simulations\n", | |
| "You won 100.0% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 3 heads out of 5 simulations\n", | |
| "You won 60.0% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 2 heads out of 10 simulations\n", | |
| "You won 20.0% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 8 heads out of 20 simulations\n", | |
| "You won 40.0% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 28 heads out of 50 simulations\n", | |
| "You won 56.00000000000001% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 39 heads out of 100 simulations\n", | |
| "You won 39.0% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 262 heads out of 500 simulations\n", | |
| "You won 52.400000000000006% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 493 heads out of 1000 simulations\n", | |
| "You won 49.3% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 5028 heads out of 10000 simulations\n", | |
| "You won 50.28% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 49942 heads out of 100000 simulations\n", | |
| "You won 49.942% of the times\n", | |
| "\n", | |
| "\n", | |
| "There were 499967 heads out of 1000000 simulations\n", | |
| "You won 49.9967% of the times\n", | |
| "\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "simulation_list = [2,5,10,20,50,100,500,1000,10000,100000,1000000]\n", | |
| "for n_simuls in simulation_list:\n", | |
| " coin_toss_simul(n_simuls)" | |
| ] | |
| } | |
| ], | |
| "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.7.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment