// custom app examaple, dice simulation
// HP Prime User Guide, Dec. 2017, page 613-615, 621-627
// reference Chapter 12 Statistics 1Var app
// This program is different from the User Manual, in that the Start function
// was move out of the View Menu functions.
#pragma mode( separator(.,;) integer(h32) )
// function declarations
ROLLDIE();
// variable declarations
EXPORT SIDES; // number of sides on each die
EXPORT ROLLS; // number of times to roll the dice
// main program
EXPORT DiceSimulation()
BEGIN
END;
// Start function
START()
BEGIN
D1:={}; // data list of each possible outcome (sum of the two dice)
D2:={}; // data list of the number of times each possible outcome occured
H1:= {'D1','D2',1,0,#FF:24h}; // statisics data set definition, note use of semi-colon
STARTVIEW(6,1); // app info view
END;
// Views key functions
VIEW "Set Rolls",SETROLLS()
BEGIN
REPEAT
INPUT(ROLLS,"Num of rolls","N=","Enter# of rolls",25);
ROLLS:= FLOOR(ROLLS);
IF ROLLS<1 THEN
MSGBOX("You must enter a num >=1");
END;
UNTIL ROLLS>=1;
STARTVIEW(7,1); // views key view
END;
VIEW "Set Sides",SETSIDES()
BEGIN
REPEAT
INPUT(SIDES,"Die Sides","N=","Enter# of sides",2);
SIDES:= FLOOR(SIDES);
IF SIDES<2 THEN
MSGBOX("# of sides must be >= 4");
END;
UNTIL SIDES>=4;
STARTVIEW(7,1); // views key view
END;
VIEW "Roll Dice",ROLLMANY()
BEGIN
LOCAL k,roll;
D1:= MAKELIST(X+1,X,1,2*SIDES-1,1);
D2:= MAKELIST(0,X,1,2*SIDES-1,1); //makelist(expression,variable,begin,end,[increment])
FOR k FROM 1 TO ROLLS DO
roll:=ROLLDIE(SIDES)+ROLLDIE(SIDES);
D2(roll-1):= D2(roll-1)+1;
END;
Xmin:= -0.1;
Xmax:= MAX(D1)+1;
Ymin:= −0.1;
Ymax:= MAX(D2)+1;
STARTVIEW(1,1); // plot view
END;
// Plot key function
PLOT()
BEGIN
Xmin:= -0.1;
Xmax:= MAX(D1)+1;
Ymin:= −0.1;
Ymax:= MAX(D2)+1;
STARTVIEW(1,1); // plot view
END;
// Symbolic key function
Symb()
BEGIN
H1:= {'D1','D2',1,0,#FF:24h}; //note use of semi-colon
STARTVIEW(0,1); // symbolic view
END;
// other functions
EXPORT ROLLDIE(N) // use the rolldie function on page 613
BEGIN
RETURN 1 + RANDINT(N-1);
END;