HP 33S Program question



#9

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


#10

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.)


#11

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

#12

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

#13

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.

#14

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.


#15

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.

#16

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.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP Prime: run a program in another program Davi Ribeiro de Oliveira 6 864 11-11-2013, 08:28 PM
Last Post: Davi Ribeiro de Oliveira
  Maximum number of program steps in HP-42S, 33S, and 35S? Walter B 3 593 12-18-2012, 03:44 PM
Last Post: Eric Smith
  42s program question David Persinger (US) 5 602 08-09-2012, 06:34 AM
Last Post: David Persinger (US)
  WP34S program question Mike Maiorana 12 1,078 04-05-2012, 03:09 AM
Last Post: Steve Simpkin
  Program Coding Sheets for 33s/35s Matt Agajanian 6 734 03-03-2012, 04:03 PM
Last Post: David Hayden
  33s - programming question Frank Rottgardt 4 570 05-15-2008, 08:27 PM
Last Post: Don Shepherd
  HP-41 MCODE: Making an MCODE program call another MCODE program Geir Isene 10 1,101 01-13-2008, 05:58 AM
Last Post: Raymond Del Tondo
  HP 33s: Addressing registers at the "Top" of the program list. Ted Madson 0 247 10-23-2007, 05:11 PM
Last Post: Ted Madson
  Program format from 33s to 35s romeo_charlie 10 907 09-16-2007, 05:02 PM
Last Post: Paul Dale
  Program question Vincze 19 1,555 08-02-2007, 09:27 AM
Last Post: Thomas Klemm

Forum Jump: