{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def one_ace(n): # Compute the probability of one ace in each hand using n samples\n", " import numpy as np\n", " rng = np.random.default_rng() # defaul numpy random number generator\n", " \n", " deck = np.arange(52) # cards will be numbered from 0 to 51\n", " aces_ok = 0 # number of successes will be stored here\n", "\n", " # To fix values, we will atribute 0, 13, 26 and 39 to the four aces\n", " # Could be any four distinct numbers in 0 .. 51.\n", " \n", " for k in range(n): # sample n times\n", " hands = rng.choice(deck, size=(4,13), replace=False) # Choose randomly the four hands without replacement\n", " \n", " # construct a four components array with the number of aces in each hand\n", " naces = np.sum(hands==0, axis=1) + np.sum(hands==13, axis=1) \\\n", " + np.sum(hands==26, axis=1) + np.sum(hands==39, axis=1)\n", " \n", " aces_ok += np.product(naces) # if there is one ace in each hand, the product is one, otherwise is zero\n", " \n", " return aces_ok / n # frequency of successes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "one_ace(100000)" ] }, { "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.6.9" } }, "nbformat": 4, "nbformat_minor": 2 }