{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# CS 237 Spring 2019\n", "# Author: Alina Ene (aene@bu.edu)\n", "# Used in L6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CS 237: Dice Game" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from numpy.random import randint\n", "import matplotlib.pyplot as plt\n", "plt.style.use('seaborn')\n", "\n", "blue_die = [0, 0, 4, 4, 4, 4]\n", "green_die = [1, 1, 1, 5, 5, 5]\n", "orange_die = [2, 2, 2, 2, 6, 6]\n", "yellow_die = [3, 3, 3, 3, 3, 3]\n", "\n", "# die1 vs die2\n", "# in each roll, the larger number wins\n", "def roll_dice(die1, die2, num_rolls = 10000):\n", " die1_wins = 0\n", " for i in range(num_rolls):\n", " n1 = die1[randint(1, 7) - 1]\n", " n2 = die2[randint(1, 7) - 1]\n", " if n1 > n2:\n", " die1_wins = die1_wins + 1\n", " return die1_wins / num_rolls" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.338\n" ] } ], "source": [ "print(roll_dice(blue_die, green_die))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3346\n" ] } ], "source": [ "print(roll_dice(green_die, orange_die))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3358\n" ] } ], "source": [ "print(roll_dice(orange_die, yellow_die))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3356\n" ] } ], "source": [ "print(roll_dice(yellow_die, blue_die))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }