Voyagers' Pseudorandom Number Generator on HP41CX

Programs for HP-41

Voyagers' Pseudorandom Number Generator on HP41CX

Postby mike-stgt » Thu Jun 25, 2020 11:00 pm

I ported the Voyagers' Pseudorandom Number Generator to the HP41CX. It could be useful when exactly the same sequence of random numbers is necessary for tests.

Warnings first
Indispensable prerequisite is an HP-41CX either as real machine or running under a so-called "emulator" (one of the CPU-simulating kind). Any former OS of 41C or 41CV will probably fail, highly likely to produce a MEMORY LOST or even worse, alter memory silently. There are no safety measures to prevent you to carry out this misuse.

Intended use
VORANOGE.zip contains one "4k ROM file" in two different formats. The .MOD file may be used with V41 and the .BINJFG may be used with Emu41. See the emulators' description for details how to "plug-in application packs".

What else?
This module comes with two commands only, SEED and RN. SEED stores a number in X to a buffer as sequence start for RN. In case there is no seed when executing RN time serves as seed (some fraction of seconds to be precise). RN computes with the seed a pseudorandom number, saves it as new seed and sets it to stack register X. Regarding stack lift RN acts like RCL.

Note: the seed buffer is volatile, PACKing or a power cycle (OFF-ON) will flush it.

Few more details
XROM: 18
FUNC: 3
VERS: FX20
IRVC: no interrupt vector
CKSM: 091 (145 dec) ok
FAT:
XROM 18,00 -VORANOGE . . B013 * NP
XROM 18,01 RN. . . . . . B11D
XROM 18,02 SEED. . . . . B10E
Buffer-ID: 9

The fine print
VORANOGE is published under QPL, the Q Public License V 1.0 -- find it also in VORANOGE.zip


Final remark: this is not a request for comments or suggestions. But feel free to report errors.

/M.

Edit: stack lift mentioned
Attachments
VORANOGE.zip
The a. m. ZIP
(4.17 KiB) Downloaded 224 times
Last edited by mike-stgt on Sat Oct 17, 2020 10:24 am, edited 1 time in total.
mike-stgt
.........
.........
 
Posts: 97
Joined: Tue Dec 24, 2019 12:12 pm

Re: Voyagers' Pseudorandom Number Generator on HP41CX

Postby mike-stgt » Wed Jul 08, 2020 11:50 pm

Addendum
A very simple application using random numbers is the Monte Carlo method. Find below how to estimate the volume of a circle with r=1 (result should be Pi),
i) on an HP-11C
Code: Select all
 1-42.21.11  LBL A
 2-       4  4
 3-      15  1/x
 4-      12  e^x
 5-   44 36  SEED
 6-      26  EEX
 7-       4  4
 8-      36  ENTER^
 9-   44 25  STO I
10-42.21. 1  LBL 1
11-   42 36  RAN#
12-   43 11  x^2
13-   42 36  RAN#
14-   43 11  x^2
15-      40  +
16-   43 44  INT
17-      30  -
18-   42  5  DSE
19-   22  1  GTO 1
20-      34  x<>y
21-      10  /
22-       4  4
23-      20  *
24-   43 32  RTN
result = 3,140000

ii) same on an HP-41CX
PiMCds.png
Pi by Monte Carlo with distinct seed
PiMCds.png (870 Bytes) Viewed 3203 times

result = 3,140000 (as expected -- when using the VoRaNoGe ROM)

iii) same once more on a 41CX running under Emu41
Pi by Monte Carlo.png

Note: I did all examples on virtual calculators like Emu41, V41 and -- for the 11C -- with HP's PC version of the HP-15C (ROM just replaced). The later is fast enough to experiment with larger iterations and different seeds.

BTW -- RN returns random numbers in the range 0 <= rn < 1. So in the a. m. example r is not 1, only close to 1. I leave this for you or the experts, to investigate if a correction is necessary.
mike-stgt
.........
.........
 
Posts: 97
Joined: Tue Dec 24, 2019 12:12 pm

Re: Voyagers' Pseudorandom Number Generator on HP41CX

Postby mike-stgt » Tue Aug 25, 2020 3:10 pm

One more use case for RN -- replacement of GN (Gaussian rN generator) found in PPC ROM.

Besides using my seed-in-buffer-RN instead of the register-based RN of PPC ROM I also like to get rid of the 'nasty surprise' (soft-crash) related to incorrect trig setting. The technical details in PPC ROM USR MNL show 'angular mode not used' what is only true in so far it remains unchanged. But you have to set DEG before using GN of PPC ROM, otherwise the results are silently! wrong. For this I first planned to get the "circular factor" according the set angular mode like this:
Code: Select all
 01 PI
 02 FC? 43
 03 R-D
 04 ,9
 05 FC? 42
 06 SIGN
 07 /

where lines 01..03 (4 bytes) would be enough for those who never-ever set their HP41 to GRAD. However, for this function in following 'replace-GN-of-PPCROM' program 3 bytes are entirely sufficient to respect any angular setting (including GRAD mode):
gn.png
gn.png (588 Bytes) Viewed 2872 times

Used formula is the same as PPC ROM. The hindmost two lines, VIEW X and VIEW Y, are to ease transfer of the result by edit/copy - edit/paste when running on virtual equipment. It's up to you to delete them or set flag 21 according your preference. On an HP-42S I would replace R06 and R07 by named ones, 'standard deviation' and 'mean' or alike.

Edit: stress angular mode
mike-stgt
.........
.........
 
Posts: 97
Joined: Tue Dec 24, 2019 12:12 pm

Full Period LCG

Postby mike-stgt » Sun May 23, 2021 5:23 pm

Just a little supplement information to my Random Number Generator copied from Voyagers.

The Voyager (HP-11C and -15C) function RAN# is of the LCG-kind (affine transformation) with the constants
voranoge.gif
Linear congruential generator (LCG) used in two Voyagers.
voranoge.gif (1.86 KiB) Viewed 964 times

The good news: these parameters comply to the Hull–Dobell Theorem and so it is a full period LCG -- for all seed values the period is 1E10.
Any bad news? This RNG shares the flaws common to LCGs. To find out which of the tests here and there it will not pass is left as an exercise to the users. Some may be quite obvious after few moments of this lecture, at the latest when: "... reasons shown here -- this is not too difficult to understand." :mrgreen:

M.
mike-stgt
.........
.........
 
Posts: 97
Joined: Tue Dec 24, 2019 12:12 pm

Addendum

Postby mike-stgt » Tue Jun 15, 2021 4:26 pm

Anyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin.
-- John von Neumann
mike-stgt
.........
.........
 
Posts: 97
Joined: Tue Dec 24, 2019 12:12 pm

Re: Voyagers' Pseudorandom Number Generator on HP41CX

Postby Garth » Tue Jun 15, 2021 6:01 pm

The cx has the time module built in. The time is kept in hundredths of seconds, and the exact time of a desired random number will not be known; so why not include the time in the generation of random numbers. It will add an element of true randomness.
Garth
Moderator
Moderator
 
Posts: 248
Joined: Tue Jan 20, 2009 1:31 am

True Randomness

Postby mike-stgt » Wed Jun 16, 2021 7:42 am

Garth wrote:...; so why not include the time in the generation of random numbers. It will add an element of true randomness.

Sure :) -- I cite from my OP:
mike-stgt wrote:In case there is no seed when executing RN time serves as seed (some fraction of seconds to be precise).
So RN serves in two ways,
i) wthout seed using the fortuity/haphazardness when you execute the function to get (most times) a different sequence at random, or
ii) with seed to repeat the same sequence for tests with no need to keep test data in a file. Using my VORANOGE.MOD this is now possible even on different machines: HP-11C, -15C, and -41CX (not C nor CV), virtual remakes included as shown here.
Last edited by mike-stgt on Wed Jun 16, 2021 10:20 am, edited 1 time in total.
mike-stgt
.........
.........
 
Posts: 97
Joined: Tue Dec 24, 2019 12:12 pm

Re: True Randomness

Postby Garth » Wed Jun 16, 2021 8:27 am

mike-stgt wrote:
Garth wrote:...; so why not include the time in the generation of random numbers. It will add an element of randomness.

Sure :) -- I cite from my OP:
mike-stgt wrote:In case there is no seed when executing RN time serves as seed (some fraction of seconds to be precise).

I had done a search for "time" and that was the only occurrence, but without a comma after "RN," it didn't make sense, so I didn't catch it. The time wouldn't have to necessarily be the seed, but rather something else brought into the equation that you apply to a seed.
Garth
Moderator
Moderator
 
Posts: 248
Joined: Tue Jan 20, 2009 1:31 am


Return to HP-41 Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron