Skip to content

Instantly share code, notes, and snippets.

@lexnederbragt
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save lexnederbragt/8869553 to your computer and use it in GitHub Desktop.

Select an option

Save lexnederbragt/8869553 to your computer and use it in GitHub Desktop.
Yahtzee simulation, based on a question on facebook: in 5 dice yahtzee game, what is the chance of getting a yahtzee (5 similar dice), ignoring all other options (straight, etc)
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Task: in 5 dice yahtzee game, what is the chance of getting a yahtzee (5 similar dice), ignoring all other options (straight, etc)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from random import randrange\n",
"from collections import Counter\n",
"import operator"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def throw(num_dices = 5):\n",
" \"\"\"Performs a throw with up to 5 dices\"\"\"\n",
" thrown = []\n",
" for i in range(num_dices):\n",
" thrown.append(randrange(6)+1)\n",
" return thrown"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def most_common(dices):\n",
" \"\"\"\n",
" Returns the dices that are most common\n",
" In case of a draw, returns the lowest number\n",
" \"\"\"\n",
" counts = Counter(dices)\n",
" keep = max(counts.iteritems(), key=operator.itemgetter(1))[0]\n",
" return [keep] * counts[keep]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def roll_dice():\n",
" \"\"\"\n",
" Roles the dice three times \n",
" Keeps the most frequent dice after each throw\n",
" Returns the most frequent dices after the final throw\n",
" \"\"\"\n",
" dices = []\n",
" for i in range(3):\n",
" dices.extend(throw(5-len(dices)))\n",
" # select most frequent\n",
" dices = most_common(dices)\n",
" return dices"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def game():\n",
" \"\"\"\n",
" Plays a game of up to 15 rolls\n",
" Tests whether yahtzee is reached\n",
" \"\"\"\n",
" win = False\n",
" for i in range(15):\n",
" dices = roll_dice()\n",
" # test for 5 (similar) dices returned\n",
" if len(dices) == 5:\n",
" win = True\n",
" return win"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below is the code that does the actual playing."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"games = 100000\n",
"wins = 0\n",
"for i in range(games):\n",
" if game():\n",
" wins += 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"Games played: %i. Games with yahztee: %i. Frequency: %f%%\" %(games, wins, 100*wins/float(games))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Games played: 100000. Games with yahztee: 50893. Frequency: 50.893000%\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we only check the chance for a Yahtzee for a set of three rolls."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"rolls = 100000\n",
"roll_wins = 0\n",
"for i in range(rolls):\n",
" if len(roll_dice()) == 5:\n",
" roll_wins += 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"Rolls (three each): %i. Rolls with yahztee: %i. Frequency: %f%%\" %(rolls, roll_wins, 100*roll_wins/float(rolls))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Rolls (three each): 100000. Rolls with yahztee: 4582. Frequency: 4.582000%\n"
]
}
],
"prompt_number": 9
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment