Pseudo Random Number Generators

The HP-34C lacks a pseudo random number generator. Let's make one ourselves.

Method 1

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 A001–25 13 11
RCL 0002–24 0Get the seed value
9003–9Multiply with a relatively high prime number
9004–9
7005–7
×006–61
h FRAC007–25 33Use only the fraction part of it
STO 0008–23 0Store as new seed
h RTN009–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 A001–25 13 11
RCL 0002–24 0Get the seed value
9003–9Multiply with a relatively high prime number
9004–9
7005–7
×006–61
h FRAC007–25 33Use only the fraction part of it
STO 0008–23 0Store as new seed
 
6009–6Multiply fraction by 6
×010–61
h INT011–25 32Results in a value from 0 to 5
1012–1Let's add 1 to make it 1 to 6
+013–51
f FIX 0014–14 11 0
h RTN015–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.

Method 2

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 A001–25 13 11
RCL 0002–24 0Get the seed value
h π003–25 73Add pi to it
+004–51
5005–5Raise it to the power of 5
h YX006–25 3
h FRAC007–25 33Keep only fraction
STO 0008–23 0Save as new seed
h RTN009–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 – ......