Skip to content

Instantly share code, notes, and snippets.

@sampathweb
Created August 16, 2020 18:01
Show Gist options
  • Save sampathweb/9325e3f0b25b06452d9eb65629b65ee1 to your computer and use it in GitHub Desktop.
Save sampathweb/9325e3f0b25b06452d9eb65629b65ee1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "simulation-prob.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "SHyBxiowQIQ8",
"colab_type": "code",
"colab": {}
},
"source": [
"import random\n",
"import numpy as np"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "P4LrKONGQhMP",
"colab_type": "code",
"colab": {}
},
"source": [
"# Roll two dice 100 times and record each occurance"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "oXXWjCNzXJvY",
"colab_type": "code",
"colab": {}
},
"source": [
"def roll_dice():\n",
" return np.random.choice([1, 2, 3, 4, 5, 6])"
],
"execution_count": 18,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "bnVX0rstXYPZ",
"colab_type": "code",
"colab": {}
},
"source": [
"sim_results = []\n",
"for i in range(30):\n",
" n = 10\n",
" results = {\n",
" 1: 0,\n",
" 2: 0,\n",
" 3: 0,\n",
" 4: 0,\n",
" 5: 0,\n",
" 6: 0\n",
" }\n",
" for _ in range(n):\n",
" roll = roll_dice()\n",
" results[roll] +=1\n",
" for roll, count in results.items():\n",
" results[roll] = count / n\n",
" sim_results.append(results)"
],
"execution_count": 73,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "kDN-CTQyXufb",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "63d7abab-03de-4e5c-97a6-8922eb74f206"
},
"source": [
"len(sim_results)"
],
"execution_count": 71,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 71
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "pM1O6td7YeZ6",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "5d0675fd-2054-431d-ceb5-1e526e9ae71c"
},
"source": [
"sim_results[29]"
],
"execution_count": 67,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1: 0.1, 2: 0.1, 3: 0.2, 4: 0.1, 5: 0.4, 6: 0.1}"
]
},
"metadata": {
"tags": []
},
"execution_count": 67
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "DP4CboHuaNRp",
"colab_type": "code",
"colab": {}
},
"source": [
"results = dict(zip(range(1, 7), [0] * 6))\n",
"for res in sim_results:\n",
" for roll, count in res.items():\n",
" results[roll] += count\n",
"for roll, count in results.items():\n",
" results[roll] = count / 30"
],
"execution_count": 68,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "xa4Kp9p6adqU",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
},
"outputId": "b7e12666-cd28-4d75-f8de-478c292ec7a9"
},
"source": [
"sim_results"
],
"execution_count": 72,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[{1: 0.16,\n",
" 2: 0.18666666666666668,\n",
" 3: 0.17333333333333334,\n",
" 4: 0.18,\n",
" 5: 0.14666666666666667,\n",
" 6: 0.15333333333333332}]"
]
},
"metadata": {
"tags": []
},
"execution_count": 72
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "GOjCS_bFavOl",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment