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 350 times
Last edited by mike-stgt on Sat Oct 17, 2020 10:24 am, edited 1 time in total.
mike-stgt
.........
.........
 
Posts: 134
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 5908 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.

Edit: A Complement
Triggered by the append from Gerson W. Barbosa in another forum's thread I am urged to complement this contribution here by the astonishing evolution of a subject I assumed to be finalized since long.

To be clear, it's neither about Pi nor Monte Carlo method, the goal is now to find the fastest program to get the same, well known result (kind of reinventing the wheel). New at the starting line is another approach, regarding the probability that fractions of random numbers will round to even integers is a matter of Pi, quite more rational than simply metering a quarter circular sector as used in the example shown above. Moreover there exist other programmable machines, of which the HP-15C seems to be (at first glance) the most comparable since it runs on the (almost) identical Nut-CPU as the HP41. To compensate the different clock speeds I do not regard the execution time, only the CPU cycle counts.

First round: and the winner is... the "rational" approach on the 41 and the "circular" approach on the 15C. Why? Well, the later lacks the MOD function and its replacement results in a one line longer program what overthrows the advantage of the less demanding "rational" approach.

What about the random numbers? Both routines use two of them in every iteration. Would not one from the previous loop still be "random enough" to be used once again? Could RCL RAN# of the 15C be advantageous? Questions like this and some workouts and exercises evolved optimized routines for the

Final round: and the winner is... the "circular" approach, on both machines.

Code: Select all
HP41CX program:     HP15C copy:
01•LBL "PIMC"        1-42,21,11  LBL A
02 STO 01            2-   44 25  STO I
03 CLX               3-       0  0      ; no need to square this one
04 STO 00            4-   44  0  STO 0
05 RCL 01            5-      34  x<>y
06 ENTER^            6-      36  ENTER^
07•LBL 01            7-42,21, 0  LBL 0
08 RCL 00            8-   45  0  RCL 0  ; previous RN^2
09 RN                9-   42 36  RAN#   ; get a new RN
10 X^2              10-   43 11  x^2
11 STO 00           11-   44  0  STO 0  ; save for future use
12 +                12-      40  +
13 INT              13-   43 44  INT
14 -                14-      30  -      ; hit-=1 if outside
15 DSE Y            15-42, 5,25  DSE I
16 GTO 01           16-   22  0  GTO 0
17 RCL 01           17-      34  x<>y
18 /                18-      10  /
19 4                19-       4  4
20 *                20-      20  *
21 END                |
   |                3842552 CPU cycles for input 1E3
   |
2187000 CPU cycles for input 1E3 if PR detached and VORANOGE.ROM on page 8.

One word about the Monte Carlo method. I intentionally omitted to predeterminate the RN sequence within the final programs, so every run will show a different result at random. This gives the chance to encounter the "nature" of Monte Carlo method and to assess its value or possibility in metering some shape depending on the number of iterations and a "nice choice" of seed for the pseudorandom number sequence. An example here: "Let's give luck a chance." Another one worth to recheck could be the paper Boldly Going - Mandelbrot Set Area (HP42S) from "Don A.", if the asserted, so much impressing accuracy may be repeated also with different seeds.

Who likes to surprise with amazing results may still set a "nicely chosen" seed before running the routines as Gerson proposes at the end of his append here.
Last edited by mike-stgt on Sun Jan 23, 2022 12:22 pm, edited 1 time in total.
mike-stgt
.........
.........
 
Posts: 134
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 5577 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: 134
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 3669 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: 134
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: 134
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: 268
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: 134
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: 268
Joined: Tue Jan 20, 2009 1:31 am

Update: Voyagers' Pseudorandom Number Generator on HP41CX

Postby mike-stgt » Sun Jan 23, 2022 12:13 am

Changed/supplemented version:

What is VORANOGE?
It is a port of the Voyagers' Pseudorandom Number Generator (built in HP-11C and -15C) to the HP41CX. It could be useful when exactly the same sequence of random numbers is necessary for tests.

What is new in "VORANOGE 3" compared to the first release?
- ROM ID changed from 18 to 13 to avoid interference with Plotter ROM,
- additional function RCLSD recalls currently stored seed (analogous to RCL RAN# available on 15C),
- additional function S21 copies the behaviour of SEED alike HP-21S (only the functionality, not the RAN# generator).
The functions are not sorted alphabetically for CAT2 not to jeopardize programs already written with version 2 of VORANOGE. Those prepared with first version need to be edited due to the ROM ID change.

Warning first
Indispensable prerequisite for this firmware 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 fail, 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 3.zip" contains the same 4k ROM in two different formats. The .MOD file may be used with V41 -- https://hp.giesselink.com/v41.htm -- and the .BINJFG may be used with Emu41 -- http://www.jeffcalc.hp41.eu/emu41/index.html See the emulators' description for details how to use application packs.

What else?
This ROM comes with only four commands, SEED, RN, RCLSD, and S21.
SEED stores the content of stack register X to a buffer as sequence start for RN.
S21 does the same but returns also the first pseudorandom number based on the given seed (which itself is copied to LastX).
RN computes a pseudorandom number based on the seed, saves it as new seed and puts it to stack register X.
RCLSD recalls the last pseudorandom number or current seed (what is the same after RN and S21, in contrast SEED might modify the input) or returns 0 if not available.
Notes:
- In case there is no seed when executing RN, some fraction of seconds of current time serve as seed.
- By design the seed buffer is volatile, PACKing or a power cycle (OFF-ON) will flush it. Buffer-ID is 9.

Few more details
For the very first time I prepare a MOD file not on a mainframe under z/VM but on a PC only with mkMOD, a programmer's text editor, and NutEm/PC as emulator. It's doable, it works, alas the details to present are not so detailed as before (in fact, following is a printout from HP41CX):
Code: Select all
XROM   FName        Adr.
13.00  -VORANOGE 3  B019
13.01  RN           B11D
13.02  SEED         B10E
13.03  RCLSD        B168
13.04  S21          B0E9

The fine print
"VORANOGE V3" is published under QPL, the Q Public License V 1.0 -- find it also in "VORANOGE V3.zip".
Please read it and proceed only if you do agree!
Attachments
VoRaNoGe V3.zip
Voyagers' Pseudorandom Number Generator on HP41CX
(4.71 KiB) Downloaded 48 times
mike-stgt
.........
.........
 
Posts: 134
Joined: Tue Dec 24, 2019 12:12 pm

Re: Voyagers' Pseudorandom Number Generator on HP41CX

Postby mike-stgt » Thu Apr 28, 2022 3:00 pm

Edited 4/30/22, 16:45 MESZ
Note: Today Àngel published an update of the description (also the metadata of the MOD) of his RANDOM ROM, now most of my remarks are obsolete and I removed them. For the remaining I kept the numbering.
=====

Àngel Martin (ÀM) copied VoRaNoGe ROM altered and partly (function S21 missing) to his recently published RANDOM ROM. Just few hints about this:

5. In the description accompanying RANDOM ROM ÀM emphasizes on the fact, that the buffer holding the seed for RN is "non-committed" even though I clearly noticed:
>> By design the seed buffer is volatile, PACKing or a power cycle (OFF-ON) will flush it. <<
So this is on purpose and no oversight.

7. Does it comply "with the system standards used across the board" that BFCAT, or ÀM, takes the liberty of writing to another ROM's buffer?

11. There is a pettily mistake regarding buffer numbers, by chance with severe consequences. Contrary to the description SEEDT uses buffer-ID #4 what (currently) interferes with the volatile buffer of SEED/RN.

Today ÀM also enhanced the description of his 'Extended Statistics Module', a considerable betterment. Alas... IMO there's still a little detail pending -- the original, unchanged program is missing. So users can't see what is changed and why, the bold highlighted 'FIX 7' in the listing of the changed version is not helping. The replaced '7FIX' enabled the user to set the required accuracy and optionally reduce with it the execution time noticeably. That was a significant characteristic of my original routine. (For practical use four digits are sufficient, full precission is for bean counters. The original was good for both.)

One more word about RAND from ToulMath-0C. Since it uses the same constants for an LCG to produce a sequence of pseudorandom numbers as my VoRaNoGe I suggest to use a (working) copy from ToulMath within RANDOM ROM and remove the obsolete VoRaNoGe. Why? It would be kind of late acknowledgement of PPC-Toulouse, where one or several members had been able to copy the Voyagers LCG (two distinct seeds and two RAN# are enough to get its constants) -- in contrast I used the HP11C ROM and portet two code snippets to an old PRNG I've done in MCode decades ago. From my point of view the PPC-T deserves the higher esteem (also the results are the same).

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


Return to HP-41 Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron