Post Reply 
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
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.
Find all posts by this user
Quote this message in a reply
12-08-2017, 07:43 PM
Post: #22
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
Find all posts by this user
Quote this message in a reply
12-08-2017, 09:17 PM (This post was last modified: 12-08-2017 09:18 PM by Trond.)
Post: #23
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.
Find all posts by this user
Quote this message in a reply
12-08-2017, 09:56 PM (This post was last modified: 12-08-2017 10:04 PM by Dieter.)
Post: #24
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
Find all posts by this user
Quote this message in a reply
12-09-2017, 09:53 PM
Post: #25
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
Find all posts by this user
Quote this message in a reply
12-10-2017, 04:09 AM
Post: #26
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! Smile OK I will try this one.
Find all posts by this user
Quote this message in a reply
Post Reply 




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