Skip to content

Instantly share code, notes, and snippets.

@jdfreder
Created March 29, 2016 17:26
Show Gist options
  • Save jdfreder/2b28a8624079c1ce61b09253c9dcff4b to your computer and use it in GitHub Desktop.
Save jdfreder/2b28a8624079c1ce61b09253c9dcff4b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Die A has sides 2, 2, 4, 4, 9, 9.\n",
"- Die B has sides 1, 1, 6, 6, 8, 8.\n",
"- Die C has sides 3, 3, 5, 5, 7, 7."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"dice = [\n",
" (2, 2, 4, 4, 9, 9),\n",
" (1, 1, 6, 6, 8, 8),\n",
" (3, 3, 5, 5, 7, 7),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from random import randint\n",
"def roll(dice):\n",
" return [x[randint(0, len(x)-1)] for x in dice]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.display import display\n",
"from ipywidgets import *\n",
"from time import sleep\n",
"import sys\n",
"\n",
"display(IntProgress(value = 50, bar_style = 'warning', description=' '))\n",
"\n",
"def simulate(dice, rolls):\n",
" bars = []\n",
" for i in range(len(dice)):\n",
" bars.append(IntProgress(min=0, max=0, value=0, description='{}>{}'.format(i,i-1 if i-1>=0 else len(dice)-1)))\n",
" display(bars[-1])\n",
" for i in range(rolls):\n",
" sleep(0.01)\n",
" result = roll(dice)\n",
" for j in range(len(dice)):\n",
" bars[j].max += 1\n",
" bars[j].value += 1 if result[j] > result[j-1] else 0\n",
"simulate(dice, 1000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment