Skip to content

Instantly share code, notes, and snippets.

@tleysh
Created December 28, 2020 20:44
Show Gist options
  • Save tleysh/c1edc6f2c25733f02ed1fd150bf3e826 to your computer and use it in GitHub Desktop.
Save tleysh/c1edc6f2c25733f02ed1fd150bf3e826 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from matplotlib import figure\n",
"from ipywidgets import interact\n",
"from ipywidgets.embed import embed_minimal_html\n",
"\n",
"# A single dice \n",
"def TheDice():\n",
" return round(np.random.uniform(low = 0.5, high = 6.5, size = None),0)\n",
"\n",
"#n-dice \n",
"def summingDice(n):\n",
" ArrayofDiceoutcomes = np.zeros(n)\n",
" for eachdice in range(0,n):\n",
" ArrayofDiceoutcomes[eachdice] = TheDice()\n",
" return sum(ArrayofDiceoutcomes)\n",
"\n",
"#Throws the set of n dice m times and stores the outcome\n",
"def TheRollingOfDice(m,n):\n",
" histo = np.zeros(m)\n",
" for i in range(0,m):\n",
" histo[i] = summingDice(n)\n",
" return histo\n",
"\n",
"# Formulates a histogram from the m trials of experimental data \n",
"#and normalises this to gain a probability distribution\n",
"def HandMadeHistogram(SimData,minimumofrange,maximumofrange):\n",
" numberofInts = []\n",
" for i in range(minimumofrange,maximumofrange+1):\n",
" numberofsingeint = np.where(SimData == i)[0]\n",
" numberofInts.append(len(numberofsingeint))\n",
" return [float(x)/sum(numberofInts) for x in numberofInts]\n",
"\n",
"\n",
"# Putting this all together - given the number of trials and the number of dice we can return \n",
"# the normalised histogram of the distribution of possible sums, the minimum possible and maximum possible sum\n",
"def FromDiceNumberToData(numberoftrials,numberofdice):\n",
" SimData = TheRollingOfDice(numberoftrials,numberofdice)\n",
" minimumofrange,maximumofrange = numberofdice,6*numberofdice\n",
" HandMadeHist = HandMadeHistogram(SimData,minimumofrange,maximumofrange)\n",
" return HandMadeHist,minimumofrange,maximumofrange"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment