HP Forums

Full Version: Looking for PRNG for HP-34C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I recently got a very pretty HP-34C from the usual place. It works very nicely; apparently the well-known tragic flaws of the series were avoided by previous owners. (I have flat-topped AA NiMHs on the way, which will be externally charged, yes yes.)

I immediately started programming it, with my usual first program, a simple dice roller. Alas, though this device is the ancestor of the HP-15C, and has many advanced features, it does not have a RAN# function. (I think the 11C and 15C were the first HPs to have that?) It also does not have the integer math which would allow a Linear Congruential Generator to be implemented; for instance, it lacks the modulo function.

So how can we make a good-enough PRNG? I looked through the program library here at the Museum for solutions for such machines as the HP-67. I found something in a game program, where the seed is retrieved from a storage register, muliplied by 997, the integer portion discarded, and the result stored again. I haven't done a lot of analysis but it doesn't appear to give very good results, though they are adequate in the game program.

So, can anybody devise a reasonably good floating-point-only PRNG? It should be suitable for at least the 34C. I hesitate to call this a 'challenge', since my solution is not actually mine, and I intend to replace it with whatever anybody comes up with that's better.

It should give a number >= 0, and less than 1. It should have results similar to rolling an unbiased 10^9-sided die, with as many digits as possible, and as long a period as possible, though if the only the first four or so digits are suitable that's OK. Naturally, it should use as few steps and registers as possible.

Thanks for considering...

Edited: 22 Sept 2012, 2:31 a.m.

I remember using xn+1 = FRC ( 9821 xn + 0.211327 ) on all of my 41C*
See for yourself if these can fit your goal.
Greetings,
Massimo

Edited: 22 Sept 2012, 5:55 a.m.

For the record: this pseudo random number generator was also mentioned in the 34C manual (or the standard applications manual). IIRC the source claimed it passed the spectral test by D. E. Knuth.

Since on a 34C this generator required eleven steps just for the two constants (or two registers, equivalent to 14 steps), I used to generate random numbers in a much simpler way: r := frac(r+pi)2. I have absolutely no idea if and how well this method passes any mathematical test, but it used to work fine for my purposes - i.e. programming games like "Mastermind". ;-)

Dieter

Not very efficent but I used to use a vairation of the above on my HP33C and later my HP34C. I think it came from the HP97 manual but I can't be sure, hopefully I've remembered the seed correctly at that is an important bit!

```001 LBL A
002 .
003 5
004 2
005 8
006 4
007 1
008 6
009 3
010 LBL 0
011 f PSE
012 9
013 9
014 7
015 x
016 g FRAC
017 GTO 0
```

It was a bit shorter on the HP33C as you didn't need to use labels which was just as well as there were only 49 program steps, so I usually entered the initial seed by hand before running the program.

Mike T.

Edited: 22 Sept 2012, 7:11 p.m.