HP41C random number generator help
12-08-2017, 04:26 AM (This post was last modified: 12-08-2017 04:33 AM by Trond.)
Post: #21
 Trond Member Posts: 139 Joined: Sep 2017
RE: HP41C random number generator help
(12-05-2017 12:30 PM)Namir Wrote:  A good and simple algorithm (used by HP since the HP-65 Stat Pacs) is:

r = frac(997 * r)

Code:
LBL "RAND"
RCL 00
997
*
FRC
STO 00
RTN

In my HHC2017 presentation about PRNGs, I pointed out that the above legacy algorithm is recommended for its speed, simplicity, and relatively good results for calculators.

Namir
I tried a version of this while trying to avoid "bad seeds". I built a program that first checks if the variable 00 has anything above 0 beyond the fifth decimal point, and if not it adds pi, then goes on to multiply with 997, does FRC and STO 00. If 00 does have a digit at the fifth decimal it will simply do the last three steps. Looks to work fine for me, but it is no longer as simple of course.
12-08-2017, 07:43 PM
Post: #22
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP41C random number generator help
(12-08-2017 04:26 AM)Trond Wrote:  I tried a version of this while trying to avoid "bad seeds". I built a program that first checks if the variable 00 has anything above 0 beyond the fifth decimal point, and if not it adds pi, then goes on to multiply with 997, does FRC and STO 00. If 00 does have a digit at the fifth decimal it will simply do the last three steps. Looks to work fine for me, but it is no longer as simple of course.

Why don't you simply do it ...right? There is a clear definition of the minimal properties that have to be met: seven decimals, and the last one must be 1, 3, 7 or 9. This can be done with the following SEED code:

Code:
LBL"SEED"
ABS
FRC
1E6
*
INT
,3
+
1E6
/
STO 00
RTN

Enter a seed, XEQ "SEED", and if neccessary the input is corrected.
Example: pi => 0,1415923

Yes, this simply sets the 7th decimal to 3 even if it was another valid digit like 1, 7 or 9 before. ;-)

Dieter
12-08-2017, 09:17 PM (This post was last modified: 12-08-2017 09:18 PM by Trond.)
Post: #23
 Trond Member Posts: 139 Joined: Sep 2017
RE: HP41C random number generator help
(12-08-2017 07:43 PM)Dieter Wrote:  Why don't you simply do it ...right? There is a clear definition of the minimal properties that have to be met: seven decimals, and the last one must be 1, 3, 7 or 9. This can be done with the following SEED code:

I guess I missed the 1, 3, 7, 9 part, but yes I meant beyond the sixth decimal (do we have this written down somewhere? You seemed uncertain too). I was thinking not to change the seed if it's not necessary, but well, not sure how often it matters. But I also wanted to replace seeds that always predictably produce numbers that start with 0 in the first few decimals. Very small seeds will always start up with 0 in their first few decimals, and this includes the seed 0.0000003 which will be common (since the variable having a 0 stored is common). Having said that, my other random number generator program is working fine so, no big issue there. It was just a bit fascinating with the simplicity of multiplying with one number.
12-08-2017, 09:56 PM (This post was last modified: 12-08-2017 10:04 PM by Dieter.)
Post: #24
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP41C random number generator help
(12-08-2017 09:17 PM)Trond Wrote:  I guess I missed the 1, 3, 7, 9 part, but yes I meant beyond the sixth decimal (do we have this written down somewhere? You seemed uncertain too).

As already noted, an article in PPC Journal V4N8 by Victor K. Heyman takes a close look at four popular PRNGs, among them the 997x generator. The article mentions 11 different tests, even some quite sophisticated ones. Here it says:

Quote:With the proper seed, this generator has a cylce of 500,000 iterations. HP chose pretty well when it selected .5284163. It is supposed to pass all standard tests, and with a blink or two, it does. That is a tough seed to remember, and worse to program, so I have found one that is as good and much easier on the user: Pi, Sin, Tan. It works, and also follows all the rules -- the seed must have seven digits after multiplying by 997, the 7th not divisible by 2 or 5. (...) However every seed that meets the rule is not good.

"Not divisible by 2 or 5" means 1, 3, 7 or 9 for the seed's last digit. And that's exactly what the 997x generator produces.

Dieter
12-09-2017, 09:53 PM
Post: #25
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP41C random number generator help
(12-08-2017 07:43 PM)Dieter Wrote:  Yes, this simply sets the 7th decimal to 3 even if it was another valid digit like 1, 7 or 9 before. ;-)

OK-OK, here is a version that adjust the input only when required.

Code:
LBL "SEED997"
ABS
FRC
1E7
*
INT
RCL X
2
MOD
-
1
+
RCL X
5
MOD
X≠0?
SIGN
ST+ X
-
2
+
1E7
/
STO 00
RTN

Example:
0,1234567 XEQ"SEED997" => 0,1234567
0,2222222 XEQ"SEED997" => 0,2222223

Dieter
12-10-2017, 04:09 AM
Post: #26
 Trond Member Posts: 139 Joined: Sep 2017
RE: HP41C random number generator help
(12-09-2017 09:53 PM)Dieter Wrote:
(12-08-2017 07:43 PM)Dieter Wrote:  Yes, this simply sets the 7th decimal to 3 even if it was another valid digit like 1, 7 or 9 before. ;-)

OK-OK, here is a version that adjust the input only when required.

Code:
LBL "SEED997"
ABS
FRC
1E7
*
INT
RCL X
2
MOD
-
1
+
RCL X
5
MOD
X≠0?
SIGN
ST+ X
-
2
+
1E7
/
STO 00
RTN

Example:
0,1234567 XEQ"SEED997" => 0,1234567
0,2222222 XEQ"SEED997" => 0,2222223

Dieter

Taking things as a challenge is a good thing! OK I will try this one.
12-21-2021, 06:25 AM
Post: #27
 Trond Member Posts: 139 Joined: Sep 2017
RE: HP41C random number generator help
Just thought I should add to this old thread that the HP-55 statistics book recommends using seed plus Pi to the power of 5 [i.e. (S+π)^5], taking the fraction part of the formula.

The calculator does not have a fraction function, so I find the way they do it a bit interesting: find the whole integer part by doing +1E9 and then -1E9 (essentially letting the calculator cut off the fraction part by squeezing it out of the screen and memory), then subtracting this whole number from the rest.
12-21-2021, 07:09 PM (This post was last modified: 12-21-2021 07:10 PM by Mike T..)
Post: #28
 Mike T. Member Posts: 129 Joined: Dec 2013
RE: HP41C random number generator help
Quote:That is a tough seed to remember, and worse to program,

Actually I find it very easy to remember, I don't think I've had to look it up once since 1977! (Once you spot the 'pattern' of the keys on the keypad and the symmetry involved you should see why it is easy to remember).

Mike T.

HP21, HP25, HP32E, HP33C, HP34C, HP10C, HP11C, HP12C, HP32S, HP22S
12-22-2021, 03:58 AM
Post: #29
 ttw Member Posts: 275 Joined: Jun 2014
RE: HP41C random number generator help
For calculators with 12 digits of precision, the following exact method will work (though the cycle is only 999,958). I can post a modification (which does double the time) to double the cycle length (though not the precision) later.

The seed is any non-zero integer.

Recurrence: X(i) = X(i-1) * 423621 mod 999959.
X(i)/999959. Will be uniformly distributed on the open interval (0-1).
12-22-2021, 07:02 PM
Post: #30
 John Keith Senior Member Posts: 1,027 Joined: Dec 2013
RE: HP41C random number generator help
(12-22-2021 03:58 AM)ttw Wrote:  For calculators with 12 digits of precision, the following exact method will work ...

Are there any 12-digit HP's that don't have a RAND function?
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)