{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The question mark `?` implements the \"roll\" function in APL.\n", "\n", "`?n` will return a number pseudo-randomly selected from the integers in `⍳` with each integer having an equal chance of being selected." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List the first 6 integers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "1 2 3 4 5 6\n", "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍳6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now generate one of them at random:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "4\n", "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try again, it has a 1 in 6 chance of being the same:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "3\n", "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you kept executing `?6`, it would be like rolling a 6-sided die over and over. But what if we have 5 dice?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "4 2 1 4 5\n", "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?6 6 6 6 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "APL functions can take arrays as arguments and return arrays as results.\n", "\n", "Here's another way of rolling 5 dice at once:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "3 2 2 2 2\n", "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?5⍴6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's look at some ideas about probability.\n", "\n", "The casino table game known as \"craps\" uses 2 6-sided dice that are added to give results in the range of 2-12. The table of all possible rolls looks like this:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "┌───┬───┬───┬───┬───┬───┐\n", "│1 1│1 2│1 3│1 4│1 5│1 6│\n", "├───┼───┼───┼───┼───┼───┤\n", "│2 1│2 2│2 3│2 4│2 5│2 6│\n", "├───┼───┼───┼───┼───┼───┤\n", "│3 1│3 2│3 3│3 4│3 5│3 6│\n", "├───┼───┼───┼───┼───┼───┤\n", "│4 1│4 2│4 3│4 4│4 5│4 6│\n", "├───┼───┼───┼───┼───┼───┤\n", "│5 1│5 2│5 3│5 4│5 5│5 6│\n", "├───┼───┼───┼───┼───┼───┤\n", "│6 1│6 2│6 3│6 4│6 5│6 6│\n", "└───┴───┴───┴───┴───┴───┘\n", "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "⍳6 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the totals are given by summing \"each\" roll, in this case using APL's \"each\" operator `¨`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "2 3 4 5 6 7\n", "3 4 5 6 7 8\n", "4 5 6 7 8 9\n", "5 6 7 8 9 10\n", "6 7 8 9 10 11\n", "7 8 9 10 11 12\n", "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "+/¨⍳6 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So first, let's create a function to compute the elements in the range of results." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "2 3 4 5 6 7 8 9 10 11 12\n", "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range←{(¯1+⍴,⍵)↓⍳+/⍵}\n", "range 6 6" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "┌──┬────┬────────────────────┐\n", "│2 │271 │⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│3 │582 │⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│4 │908 │⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│5 │1099│⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│6 │1387│⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│7 │1676│⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕│\n", "├──┼────┼────────────────────┤\n", "│8 │1368│⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│9 │1087│⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│10│748 │⎕⎕⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│11│593 │⎕⎕⎕⎕⎕⎕⎕ │\n", "├──┼────┼────────────────────┤\n", "│12│281 │⎕⎕⎕ │\n", "└──┴────┴────────────────────┘\n", "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "graph←{⍉↑t n('⎕'⍴⍨¨⌊0.5+20×{⍵÷⌈/⍵}n←+/(t←∪,+/¨⍳⍵)∘.=+/¨?⍺⍴⊂⍵)}\n", "10000 graph 6 6" ] } ], "metadata": { "kernelspec": { "display_name": "Dyalog APL", "language": "apl", "name": "dyalog-kernel" }, "language_info": { "file_extension": ".apl", "mimetype": "text/apl", "name": "APL" }, "tryapl": { "category": "Closer Looks at Some Functions", "description": "Taking APL for a Roll", "name": "Random Numbers: ?" } }, "nbformat": 4, "nbformat_minor": 2 }