HP Forums
DiceSimulation - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: DiceSimulation (/thread-8541.html)



DiceSimulation - Pieter - 06-21-2017 10:23 AM

I'am trying to understand how to create your own apps. I look at the DiceSimulation example but I do not get to let this work properly. I know that the H1Type command is no longer valid but I can't figure out how to use the alternative H1(#):=# Is there documentation available about this function?
Can someone give me advise please?
Best regards,
Pieter


RE: DiceSimulation - Gene222 - 01-19-2020 07:39 PM

This is a reply to a long dead post, but I was also struggling with the Dice Simulation Custom App example in the HP User Guide, 2017, page 621-627.

Both of the H1 variable definitions were missing a semi-colon at the end of the line.

The function DICESIMVARS on page 626 is not used in the program and should be deleted. Also delete the DICESIMVARS function declaration at the beginning of the program. Insert the ROLLDIE function (page 623) at the end of the program.

The histogram on page 627 is for 7 rolls of the dice. If you use 100 rolls as noted in the program, the histogram will be more shaped like a bell curve.

A corrected program code is shown below. Annotations were added. Some minor changes to the program code were made for clarity. [1/20/2020 The program was revised to include the Info text. The program does not work quite well without the text. The Start function was moved out of the View (menu) functions, and the order of the remaining View functions were reorganized. These changes seem to make the program work as intended by the User Manual and as described in the Info instructions. The app files are attached in the following post.]

PHP Code:
// 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 RANDINT(N-1);
END



RE: DiceSimulation - rprosperi - 01-19-2020 08:49 PM

Thanks for these corrections and sharing this here, this is useful for folks learning PPL programming.

However, it still gets a syntax error on the first H1 assignment, although that could be due to some obscure (as most are) setting.

I simply copied the code above, pasted into the virtual Prime (running the latest 20200116 f/w) and hit Check.


RE: DiceSimulation - Gene222 - 01-19-2020 09:20 PM

I ran the app on software version 2.1.14181 (2018 10 16) and hardware version A, although I ran the program on the virtual calculator. You have to create a copy of the 1 variable statistics app and copy the program to that app. Attached is the app file. {1/20/2020 The program was revised to include the Info text. The program does not work quite well without the text. The Start function was moved out of the View (menu) functions, and the order of the remaining View functions were reorganized. These changes seem to make the program work as intended by the User Manual and as described in the Info instructions.}


RE: DiceSimulation - rprosperi - 01-20-2020 02:00 AM

Ahh, ok, thanks for explaining; I should have read the OP and the manual section more closely. I'll download the modified App and check it out this week.

Thanks.