Online File

#### Chapter 12 Program 12f Stochastic simulation of board game

```*
Professional SAS Programming Secrets
Program 12f
Stochastic simulation of board game
*;

data work.boardsim (keep=turns n);
*
Initialize counters and random number stream.
*;
array counter{20} (20*0);
call streaminit(5);

do trial = 1 to 100000;
*
Initialize trial.
*;
position = 0;
round = 0;

do turn = 1 to 20;
*
Roll dice and advance. Take extra rolls if the dice match.
*;
do until (random_die1 ne random_die2);
random_die1 = floorz(rand('uniform')*6 + 1);
random_die2 = floorz(rand('uniform')*6 + 1);
random_2dice_sum = random_die1 + random_die2;
position + random_2dice_sum;
if position >= 40 then do;
*
Reset position and count round.
*;
position + -40;
round + 1;
end;
end;
if round >= 1 then leave;
end;
*
Count result.
*;
counter{turn} + 1;
end;

*
Save results.
*;
do turns = 1 to 20;
n = counter{turns};
if n > 0 then output;
end;
run;
options nocenter nodate nonumber;
title1 'Board Game Simulation: Results';
proc print data=work.boardsim;
id turns;
run;
title1 'Board Game Simulation: Statistics';
proc summary data=work.boardsim print n mean std min max;
var turns;
freq n;
run;
``` 