jrpn icon indicating copy to clipboard operation
jrpn copied to clipboard

JRPN15C random number generator different from original HP-15C

Open fcrvincent opened this issue 8 months ago • 6 comments

This is obviously a very minor issue, but the JRPN15C random number generator is different from that of the original HP-15C.

For example entering 0.5 STO RAN# and then pressing f RAN# returns 0.601798 on the original calculator but 0.825514 on the JRPN15C

The original random number generator is Seed(n+1)=FRAC(15743.52261×(100000×Seed(n))+0.1017980433) in case you like to update the app.

fcrvincent avatar Apr 12 '25 11:04 fcrvincent

Morning! Not a dev on this project, but I looked into it out of curiosity.

I don't know enough about the implementation to comment on it yet, but it appears that this might have been deliberate:

https://github.com/zathras/jrpn/blame/main/lib/jrpn15/model15c.dart#L253-L269

penryu avatar Apr 12 '25 13:04 penryu

OK I see from https://github.com/zathras/jrpn/blob/main/lib/jrpn15/model15c.dart, that the developer chose to use the dart.Random() function instead of implementing the original HP-15C simple random generator formula.

fcrvincent avatar Apr 12 '25 13:04 fcrvincent

The original random number generator is Seed(n+1)=FRAC(15743.52261×(100000×Seed(n))+0.1017980433) in case you like to update the app.

FWIW, this is exactly the sort of question I've been asking as I consider implementing the HP-16, so will keep this in mind. Thanks for this info @fcrvincent!

penryu avatar Apr 12 '25 13:04 penryu

Thanks; by the way you did an amazing job both on the JRPN15C and 16C that are close to emulator level. And the 15C's ability to load Torsten's great PC simulator formatted 15C programs is a real plus as most programs available for the 15C are published in this format now.

fcrvincent avatar Apr 12 '25 14:04 fcrvincent

Agreed! I am literally in awe of the work @zathras has done on this entire project!

penryu avatar Apr 12 '25 14:04 penryu

Agreed! I am literally in awe of the work @zathras has done on this entire project!

I second this and many thanks to @zathras giving us such great useful tools (and nice toys). There is very reasonable explanation why RAN# is working this way.

KR P

pane avatar Apr 12 '25 17:04 pane

Thanks, all.

Yep, as discussed, the difference in the random algorithm is intentional. Thanks for the orginal algorithm, @fcrvincent. Do you know if it's been published by someone with the rights to publish, in a non-copyrighted form? I tend to be a stickler about trying to not infringe IPR, so if, say, someone got the algorithm by reverse-engineering the ROM contents and then they published their findings to a discussion board, then I believe that's still proprietary IPR.

Interesting that the original algorithm is so simple. I don't know anything about pseudo-random number generation... I wonder how good the original is?

zathras avatar May 23 '25 20:05 zathras

I guess I can put this to bed now, as "will not fix" - exactly matching the behavior of the original is an explicity non-goal. Note also that integrate and solve, in particular, are unlikely to produce identical results to the original.

zathras avatar May 25 '25 17:05 zathras

Do you know if it's been published by someone with the rights to publish, in a non-copyrighted form?

Interesting that the original algorithm is so simple. I don't know anything about pseudo-random number generation... I wonder how good the original is?

Not asking for implementation - only questions above triggered my interest.

I do not know too much about random and pseudo random generators either (except for PGM 15 of ML01 for my TI58C way back then and I knew that if you wanted really random number, until recently the Fourmilab used to have the service where John Walker generated random number with Geiger counter for you). I get interested in HP implementation of pseudo random generators in calculators via this issue, so I did some search of HP official documentation and beside many reverse engineering discussions, here are some results.

The general formula is LCG Linear congruential generator (Wiki) which appears in various HP Handbooks and manuals with different constants and apparently it evolved over the time to gain grater nonrepeating span. The quality seems to be sufficient for the calculator use and it is explained in the detail (above my math capability) in The art of Computer Programming - Volume 2, D.E. Kunth.

Although I did not find exact reference and constants for HP15C in any HP official document, once you assume the formula is used in HP15C as well - since it is used in documentation and program examples - testing it and deriving constants is possible using 0 and very small seeds.

Some appearances of the formula in the official HP documents I found: HP-97 Standard Pack, pg. L13-01 (https://literature.hpcalc.org/community/hp97-pac-standard-en.pdf) HP-41C Standard applications, pg. 24 (https://literature.hpcalc.org/community/hp41-standard-en.pdf) HP-34C Applications, pg. 57 (https://literature.hpcalc.org/community/hp34c-apps-en.pdf) HP-33E Owner's handbook and programming guide, pg. 66 HP-12C Solutions Handbook, pg. 122 (https://h10032.www1.hp.com/ctg/Manual/c00367122.pdf)

Earliest non official HP reference with exact constants I found is from 85 in JPC -21 journal.

KR P

pane avatar May 26 '25 09:05 pane

Obviously it's your call @zathras.

That said JRPN15C is indeed a simulator of the HP-15C, like for example Torsten Manz also excellent (PC only) simulator. And it's reasonable to expect its behaviour to be as close as the device it's trying to simulate.

fcrvincent avatar May 26 '25 11:05 fcrvincent