The question mark ?
implements the "roll" function in APL.
?n
will return a number pseudo-randomly selected from the integers in ⍳
with each integer having an equal chance of being selected.
List the first 6 integers
⍳6
Now generate one of them at random:
?6
Try again, it has a 1 in 6 chance of being the same:
?6
If you kept executing ?6
, it would be like rolling a 6-sided die over and over. But what if we have 5 dice?
?6 6 6 6 6
APL functions can take arrays as arguments and return arrays as results.
Here's another way of rolling 5 dice at once:
?5⍴6
Now let's look at some ideas about probability.
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:
⍳6 6
And the totals are given by summing "each" roll, in this case using APL's "each" operator ¨
+/¨⍳6 6
So first, let's create a function to compute the elements in the range of results.
range←{(¯1+⍴,⍵)↓⍳+/⍵}
range 6 6
graph←{⍉↑t n('⎕'⍴⍨¨⌊0.5+20×{⍵÷⌈/⍵}n←+/(t←∪,+/¨⍳⍵)∘.=+/¨?⍺⍴⊂⍵)}
10000 graph 6 6