The HP-34C lacks a pseudo random number generator. Let's make one ourselves.
Let's start by using method copied from the dice program in the HP-34C Owner's handbook and programming guide. This random number generator needs a seed value to calculate the next pseudo random number, which then becomes the new seed. The seed can be any number from just above 0 to 1. Better said, the fraction part of the seed may not be 0. The integer part will be ignored.
000– | |||
h LBL A | 001– | 25 13 11 | |
RCL 0 | 002– | 24 0 | Get the seed value |
9 | 003– | 9 | Multiply with a relatively high prime number |
9 | 004– | 9 | |
7 | 005– | 7 | |
× | 006– | 61 | |
h FRAC | 007– | 25 33 | Use only the fraction part of it |
STO 0 | 008– | 23 0 | Store as new seed |
h RTN | 009– | 25 12 |
You'll first have to prime the seed, let's take π and store it in register 0. Then you can press the A key repeatedly and you'll get these values:
0.1679 – 0.3724 – 0.2549 – 0.1193 – 0.9900 – 0.9861 – ......
The reason why you'll get the same sequence is because we both started with the same seed. That's why it's called a pseudo random number generator. Simply store another (non zero positive value up to 1) in register 0 and you'll get a different sequence.
You can easily turn this program into a die rolling program by multiplying the new random number by 6 and adding 1 to the result. Let's try it.
000– | |||
h LBL A | 001– | 25 13 11 | |
RCL 0 | 002– | 24 0 | Get the seed value |
9 | 003– | 9 | Multiply with a relatively high prime number |
9 | 004– | 9 | |
7 | 005– | 7 | |
× | 006– | 61 | |
h FRAC | 007– | 25 33 | Use only the fraction part of it |
STO 0 | 008– | 23 0 | Store as new seed |
6 | 009– | 6 | Multiply fraction by 6 |
× | 010– | 61 | |
h INT | 011– | 25 32 | Results in a value from 0 to 5 |
1 | 012– | 1 | Let's add 1 to make it 1 to 6 |
+ | 013– | 51 | |
f FIX 0 | 014– | 14 11 0 | |
h RTN | 015– | 25 12 |
Let's store π in register 0 again and test the program by repeatedly pressing the A key.
2. – 3. – 2. – 1. – 6. – 6. – 2. – ......
This is only a very simple pseudo random number generator. It is barely good enough to create a die rolling program. The prime number we use is rather low, so if you want a random number between 0 and 1000 for instance, this generator is not of much use.
The previous method was rather primitive.
On the positive side, it is probably the fastest random number generator possible on the HP-34C.
Now I'm going to present a better one.
I've seen this one being used in several games on a variety of calculators, so I have no idea what the original source of this piece of code is.
Again this routine requires a seed value, which can be any positive value below 1, including 0 this time.
This seed value is stored in register 0.
000– | |||
h LBL A | 001– | 25 13 11 | |
RCL 0 | 002– | 24 0 | Get the seed value |
h π | 003– | 25 73 | Add pi to it |
+ | 004– | 51 | |
5 | 005– | 5 | Raise it to the power of 5 |
h YX | 006– | 25 3 | |
h FRAC | 007– | 25 33 | Keep only fraction |
STO 0 | 008– | 23 0 | Save as new seed |
h RTN | 009– | 25 12 |
The output of the program will always be a positive number below 1. You can multiply the output with a constant and an offset to it to get values in any range you would like. For instance, multiply the random number by 6, add 1 and take the integer to get values from 1 to 6, like in our fist method.
Let's try the output of our program. First store a seed of 0 to register 0, then repeatedly press the A key:
0.0197 – 0.7281 – 0.7021 – 0.9391 – 0.5613 – 0.2005 – 0.9726 – ......