Online File

#### Chapter 84 Program Risk (stochastic simulation)

```data work.risk1 (keep=d_dice a_dice outcome compress=no);
length d_dice a_dice 3 outcome \$ 2;
retain n 10000;
call streaminit(7);
do d_dice = 1 to 2; * Number of defending dice;
do a_dice = 1 to 3; * Number of attacking dice;
armies = a_dice min d_dice; * Number of armies decided;
do i = 1 to n;
roll_d1 = ceil(6*rand('uniform')); * Defending dice roll;
if d_dice >= 2 then roll_d2 = ceil(6*rand('uniform'));
else roll_d2 = 0;
defend1 = largest(1, of roll_d1-roll_d2); *Defending dice, sorted;
defend2 = largest(2, of roll_d1-roll_d2);
roll_a1 = ceil(6*rand('uniform'));   * Attacking dice;
if a_dice >= 2 then roll_a2 = ceil(6*rand('uniform'));
else roll_a2 = 0;
if a_dice >= 3 then roll_a3 = ceil(6*rand('uniform'));
else roll_a3 = 0;
attack1 = largest(1, of roll_a1-roll_a3);
attack2 = largest(2, of roll_a1-roll_a3);
* Count results;
if armies = 1 then select (attack1 > defend1);
when (1) outcome = ' A';
otherwise outcome = ' D';
end;
else select ((attack1 > defend1) + (attack2 > defend2));
when (2) outcome = 'AA';
otherwise outcome = 'DD';
end;
output;
end;
end;
end;
stop;
run;

proc tabulate data=work.risk1;
class d_dice a_dice outcome;
table d_dice*a_dice, outcome*rowpctn*f=7.3
/ rtspace=15 box='A=Attacker D=Defender';
keylabel rowpctn=' ';
run;
```