From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: Stupid pet tricks with RAND
Date: 2 Dec 1998 06:11:59 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 259
Message-ID: <742lnf$f4p$1@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:19252

Stupid pet tricks with RAND (but will they fool "The Great Randi" ?)

To warm up, before the main act, we might review how to
give the RAND function a "random spin," so that it
can be started off in a randomized place, instead of
always producing the same sequence after the calc memory
has been cleared (as in ON+A+F+Yes, but not after mere warmstarts):

HP recommends 0 RDZ to set a time-dependent starting value for RAND;
however, this then only uses the low-order 20 bits of TICKS, converted
to an integer, as its actual argument.  Said value not only re-cycles
itself every two minutes or so, but also tends to leave the last six
digits of the first subsequent RAND output almost always the same,
which I think would make Bob Wheeler very angry :)

With proper choice of an RDZ argument, however, you can create up to
1E14 different possible starting values for RAND, so you can do
much better than 0 RDZ if you want a much more varied time-dependent
starting value; the following are based upon the fact that for
arguments > 1, RDZ utilizes all 12 mantissa digits and the final
two digits of the exponent (power of 10) of the argument:

Suggestions for an RDZ0 function (to replace 0 RDZ):

Steve VanDevender suggested using TICKS mod 1E14:

\<< RCLF 8 SQ STWS TICKS 2 ALOG DUP2 / DUP 4 ROLLD * - B\->R ALOG
    SWAP 6 ALOG SQ DUP2 / * - B\->R * RDZ STOF \>>

I use a highly non-linear generator of 14 digits:

\<< DATE FACT TIME EXP * 0 RDZ RAND 2 ALOG * ALOG * RDZ \>>

The 100-year internal clock is only about 2.5E13 ticks, so a 14-digit
"seed generator" can never go through all possible starting values.

Since the internal clock is updated 8192 times per second,
there is also no way to obtain the same result twice,
even if you repeat RDZ0 as rapidly as possible, in a running program.

Since RAND always maintains an odd number in its internal
15-digit mantissa value, RAND can never produce a result
exactly equal to zero; truncation (rather than rounding) to a
12-digit result also ensures that RAND never outputs exactly 1.

The smallest and largest possible outputs from RAND
are therefore 1E-15 and .999999999999

In case you think that you will never live to see the rare event
of catching these values appearing as output, however, try this:

5.13684117130E47   RDZ  RAND RAND RAND RAND ==> .000000000000001

4.39513938817E18   RDZ  RAND RAND           ==> .999999999999999

Another peculiarity of RAND is that once you have generated some
"random" outputs, there seems to be no way to "go backwards"
and "replay" them, to produce the same effects once again.

But have no fear, The Great Randi was here, and left us a
"reverse RAND" function, below, which runs RAND backwards
from any given point, to wherever you want it to "back up" to,
after which it will once again "play out the exact same hand"
when "going forwards" with RAND again, as it had done earlier.

The HP15C calculator had one more random-number function than does
the HP48, and that function was "recall current random number,"
which delivered again the *same* output value as did the
most recent RAND.  One of the purposes of this function was
to let you *save* the exact current position in the output
sequence of RAND, so that you could *interrupt* some process or
simulation, say, then use RAND for something else for a while,
and then later resume it for the interrupted program, exactly
where it had left off, by supplying the saved value to the HP15C's
"store random number" function, which was comparable to the
HP48's RDZ (set random number seed), except that there was
no clock in the HP15C, so that you could not "randomly start"
the random number generator by using the date and time, other than
by looking at your own clock, and digitizing its output manually.

It was possible for the HP15C to resume an interrupted RAND sequence
exactly where it was, because the internally saved "random number"
mantissa was the same 10-digit length as was available in all
user-accessible storage registers and the stack; the HP48
does *not* permit you to do this, however, because the
internally saved mantissa is 15 digits long, but it gets
truncated to 12 digits when RAND outputs a value, and then
once again, there are only 12 user-specified digits in
"real" numbers that you can offer as an argument to RDZ;
to add one more coup de grace to all this, RDZ also always
forces the 15th internal mantissa digit to be a 1,
no matter what else you might have liked it to be.

However, new functions are supplied below for the HP48 which can
both recall and restore the current internal RAND value to its full
15-digit precision, using a "Long Real" (extended precision) object,
which happens to contain a 15-digit mantissa, so now you can do
on an HP48 the same things that you used to be able to do
many years earlier, on the good old sturdy HP15C, which was the first
HP calc with complex-valued functions and matrices, to accompany
the already "old hat" numeric Solve and Integration functions.

For you young folk, that model was made back when there were
all-metal keyboards which didn't wear out, because quality
electronic devices were not made as throw-aways.

Come to think of it, that was only about fifteen or so years ago,
which demonstrates how technology has so quickly progressed, to
make things which now are already obsolete or dead when shipped :)

So, with much ado about nothing much,
here are the HP48 functions for "reverse RAND"
and "recall/store current random number"
(plus an extra free bonus function, not even advertised above):

Oh, I should first mention that since these are written in ML
by an idiot, using unsupported entry points and a wild guess
that they ought to work as well on an S/SX, you might want to
back up your memory before trying them (the Federal Trade Commission
requires me to make these statements, which would be unnecessary
if only I were selling tobacco or genetically engineered foods :)

@ RANPREV:        ==> previous_rand_value               S/SX/G/GX
"CCD20E80008FB9760051B2CFA216F14613615AEAC097C713F335380333005999
0AFAAF1AFC3F30832198329935906600A70A0E59FBF10C54F158EAFC2ECC90DA
0CCB9155FAD48DD32A25CFE" @ 73.5 bytes, #EFC5h

@ RANRCL:         ==> "Long Real" [unnormalized]        S/SX/G/GX
"CCD20640008FB97601B2CFA216F14613615AEAC0AF805D0CC959908D532A2344
03008D04F01F4B5" @ 37.5 bytes, #5B4Fh

@ RANSTO:        "Long Real" [unnormalized] ==>         S/SX/G/GX
"D9D20D29512BF810DB46CCD20130008F060A2AF4808501B2CFA216F146136158
E048D34150B2130C093" @ 39.5 bytes, #390Ch

@ RANXXX:  "Mystery bonus" function (run it, and then try RAND)
@ No arguments, no results, 29.5 bytes, #9019h
"CCD20630008FB97601B2CFA216F146136AF0808205814814158E8D341509109"

The above programs are in ASC format; see Goodies Disk #1
at <www.hpcalc.org>, or see <http://www.engr.uvic.ca/~aschoorl/faq/>,
or TASC.EXE on Goodies Disk #8 to convert on PC, instead of in HP48.

Since the clueless Food and Drug Administration requires disclosure
of all ingredients (except, of course, for genetically engineered
organisms, which are not present herein),
here is the "source code" for the above objects
(you can skip this if you normally don't read labels :)

* RANPREV:   Get *previous* RAND value
* (identical to normal RAND except for the inverse multiplier)
CODE
	GOSBVL	=SAVPTR
	SETDEC
	D0=(5)	=%RAN ( find RANDOMSEED address from %RAN function )
	D0=D0+	16
	C=DAT0	A ( #80822 for G[X], #706A4 for S[X] )
	CD0EX
	A=DAT0	15
	A=0	S
	?A#0	W
	GOYES	nz
	LCHEX	0999500333083533
	A=C	W
nz	B=0	W
	ABEX	W
	LCHEX	0953992389123803 ( originally 0002851130928467 )
	GOTO	lp2
lp1	A=A+B	W
lp2	C=C-1	P
	GONC	lp1
	BSL	W
	P=P+1
	GONC	lp2
	DAT0=A	15
	ABEX	W
	P=	14
	A=A-1	A
lp3	?B#0	P
	GOYES	end
	A=A-1	A
	BSL	WP
	GONC	lp3
end	A=B	M
	GOVLNG	=PUSH%LOOP
ENDCODE

* RANRCL:  Recall current random number as unnormalized Long Real
CODE
=PUSH%%LOOP	=	#2A235 ( unsupported, appears stable )
	GOSBVL	=SAVPTR
	D0=(5)	=%RAN
	D0=D0+	16
	C=DAT0	A
	CD0EX
	A=DAT0	15
	A=0	S
	B=A	W
	SETDEC
	A=0	A
	A=A-1	A
	?B=0	M (detect the situation mentioned below)
	GOYES	nearzer
	GOVLNG	=PUSH%%LOOP
* PUSH%%LOOP in turn calls PUSH%% (2A24B), which tests for
* a non-zero *long* mantissa in B via ?B#0 M, rather than ?B#0 W;
* this misses testing the last 3 of the 15 digits; therefore
* unnormalized values less than 1E-12 will be considered zero!
* (could be fixed by normalizing/denormalizing, but I don't bother,
* since the probability of such a too low value is too low anyway :)
nearzer	LC(5)	#304 ( Undefined Result if zero or *almost* zero)
	GOVLNG	=GPErrjmpC
ENDCODE

* RANSTO:  Restore current random number from unnormalized Long Real
* (presumably produced by the complementary function above)
::
CK1NoBlame
CK&DISPATCH1
SIXTYTHREE ( 3F, Long Real )
CODE
=POP1%%		=	#2A060 ( unsupported, appears stable )
	GOSBVL	=POP1%% ( includes SAVPTR, does SETDEC )
	A=B	W ( mantissa only, ignore exponent and sign )
	ABIT=1	0 ( reduce the effects of any inappropriate inputs )
	D0=(5)	=%RAN
	D0=D0+	16
	C=DAT0	A
	CD0EX
	DAT0=A	15
	SETHEX
	GOVLNG	=GETPTRLOOP
ENDCODE
;

* RANXXX:  Run this, and then observe the results of RAND
* ( after which you may lose faith in statistics, if you ever had any :)
CODE
	GOSBVL	=SAVPTR
	D0=(5)	=%RAN
	D0=D0+	16
	C=DAT0	A
	CD0EX
	A=0	W
	LAHEX	5
	ASRC
	ASRC
	DAT0=A	15
	GOVLNG	=GETPTRLOOP
ENDCODE
* The above function is solemnly dedicated to Dr. Albert Einstein,
* who asserted:  "God does not play dice with the universe"

Finally, quoting the celebrated philanthropist P. T. Barnum:

"This way to the Egress:"

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>