The Museum of HP Calculators

HP Forum Archive 14

[ Return to Index | Top of Index ]

HP 33S Program question
Message #1 Posted by Mike on 12 May 2004, 9:30 a.m.

HP 33S Programming question “Dice roll”

It has been 20 years since I tried any programming. That was back in college when I had a 41cv and a lot more free time. Any way, I want to simulate roll of two dice, and then display both values on the screen (X and Y registers).

I wrote this program and it works fine;

D0001 LBL D Label D for Dice D0002 RANDOM Generate Random number for seed D0003 SEED Input seed D0004 RANDOM Generate random number for dice (die) D0005 6 D0006 * D0007 1 D0008 + D0009 IP Take integer portion

D0010 RANDOM Roll second dice (die) D0011 6 D0012 * D0013 1 D0014 + D0015 IP D0016 STOP

My question is how could I reduce the program length.

I know it is not a big deal for this program, but would be very helpful for a larger program. Thanks Mike

      
Re: HP 33S Program question
Message #2 Posted by Paul Brogger on 12 May 2004, 10:07 a.m.,
in response to message #1 by Mike

The obvious thing to do is make the repetitive die rolls into a single subroutine, and execute them via XEQ:

D0001 	LBL D	  Label D for Dice
D0002 	RANDOM	     Generate Random number for seed
D0003	SEED
D0004   XEQ  R       Roll one die
D0005   XEQ  R       Roll the other
D0006   RTN             (Like STOP when executed from kbd)

R0001 LBL R Roll Die R0002 RANDOM Generate random number for die R0003 6 R0004 * R0005 1 R0006 + R0007 IP R0008 RTN

This is a good technique in general, and has many applications.

Remember, however, that on the 33s an unusual set of tradeoffs may be at play. Its most limited resource is LBL values -- you've only got 26 of 'em. So in this particular case, saving a few bytes may not be worth the cost of a label. (That is, if you plan to fill your calculator with programs and will use as many labels as possible.)

            
Re: HP 33S Program question
Message #3 Posted by Bram on 12 May 2004, 11:17 a.m.,
in response to message #2 by Paul Brogger

I think that lines D0005 and D0006 can be removed for an even shorter program.

            
Re: HP 33S Program question
Message #4 Posted by r. d. baertschiger on 12 May 2004, 3:02 p.m.,
in response to message #2 by Paul Brogger

The following should also work:

D0001 	LBL D	  Label D for Dice
D0002 	RANDOM	  Generate Random number for seed
D0003	SEED
D0004   XEQ  R    Roll one die
R0001   LBL  R    Roll Die, fall into next roll of die.
R0002	RANDOM	  Generate random number for die
R0003	6
R0004	*
R0005	1
R0006	+
R0007	IP
R0008   RTN
R0009   RTN
                  
Re: HP 33S Program question
Message #5 Posted by Richard Garner on 12 May 2004, 4:07 p.m.,
in response to message #4 by r. d. baertschiger

The following should also work:

D0001 	LBL D	  Label D for Dice
D0002   XEQ  R    Roll one die
R0001   LBL  R    Roll Die, fall into next roll of die.
R0002	RANDOM	  Generate random number for die
R0003	6
R0004	*
R0005	1
R0006	+
R0007	IP
R0008   RTN
R0009   RTN

Unless the previous line D0002 RANDOM and line D0003 SEED are necessary for the program to work, this should be it. The calculator will generate 10,000,000 random numbers off of a single seed that the calculator stores in nonvolatile memory. If there is no initial seed, as in a system reset, the calculator will generate its own random number seed.

                  
Re: HP 33S Program question
Message #6 Posted by Paul Brogger on 12 May 2004, 7:29 p.m.,
in response to message #4 by r. d. baertschiger

Why two RTN's? Falling into R for its second execution will cause the first RTN to be executed as a RTN (or STOP) for program D. You don't need R0009.

                        
Re: HP 33S Program question
Message #7 Posted by Charles Perry on 13 May 2004, 10:35 a.m.,
in response to message #6 by Paul Brogger

I tried the version suggested by Richard Garner, but without R0009. It works fine. As you said, I don't think the second RTN is needed.

      
Re: HP 33S Program question
Message #8 Posted by Richard Garner on 12 May 2004, 10:08 a.m.,
in response to message #1 by Mike

I don't have a 33S, but you could remove line 2 and 3 to make it shorter. Most HP calculators will hold and use a given seed to generate about 10^7 random numbers before repeating. Next you might look at setting up a loop to calculate both die rolls eliminating duplication of code. I did something similar when I wrote a program to generate totals from a given number of die rolls for the 42S.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall