The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

HP17b & it's offsprings - Random Number Generator - How?
Message #1 Posted by Peter A. Gebhardt on 2 Dec 2009, 12:30 p.m.

Loooking into the "HP-17B Business Finance and Accounting (Step-by-Step Solutions)" on the new v7 DVD set which arrived today here in Germany (Thx. to Dave & all contributors!), I realized for the first time, that neither the keyboard nor the solver do offer a "Random Number" function.

One might argue: "If you didn't know it by now, you even might never need it ..." ;-)

Anyway. Could anybody recommend a good solver equation to generate random numbers?

Best regards,

Peter A. Gebhardt

Edited: 2 Dec 2009, 12:31 p.m.

      
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #2 Posted by Don Shepherd on 2 Dec 2009, 12:54 p.m.,
in response to message #1 by Peter A. Gebhardt

Peter, this may not be exactly random, but in the absence of any timer function, I prompted the user to enter any old multi-digit number, then took LN of that and used FP and IP to get the first 2 digits after the decimal point. That gave me a number between 00 and 99, and that's what I wanted. Like I said, it may not be "purely" random, but I got what I needed.

            
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #3 Posted by Peter A. Gebhardt on 2 Dec 2009, 1:32 p.m.,
in response to message #2 by Don Shepherd

Don,

Thx. for your quick response.

Best regards,

Peter A. Gebhardt

      
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #4 Posted by Vieira, Luiz C. (Brazil) on 2 Dec 2009, 1:14 p.m.,
in response to message #1 by Peter A. Gebhardt

Halo, Peter; wie geht´s?

I was interrupted while writing my followup so I did not see Shepherd´s wise and practical suggestion. Anyway, you can search the MoHPC set and, under HP25 Applications or HP33/34 Applications and some others, you'll find many pseudo Random number generators. They mainly use an updated seed (which happens to be the last generated random number) to compute a new one. Although the HP28S has a RAND function (also a pseudo-number generator), it offered a way to enhance the randomizing process because it allowed the internal clock to be read and placed in the stack as a number, hence you could use this value as a seed to change the Random number. I am not sure if the HP17B has a time function which returns current clock as a number, but I know that it has a clock. If possible, you could use the current clock as a seed to generate a asynchronous pseudo Random series.

Hope this helps.

Luiz (Brazil)

Edited: 2 Dec 2009, 1:16 p.m.

            
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #5 Posted by Peter A. Gebhardt on 2 Dec 2009, 1:30 p.m.,
in response to message #4 by Vieira, Luiz C. (Brazil)

Luiz,

Muito bom! Graįas ā sua ajuda.

Thank you. That's a good starting point.

Peter A. Gebhardt

PS: I must confess, I've cheated by using Google translate. Hopefully the translated sentence doesn't only look like Portuguese ... ;-)

                  
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #6 Posted by Vieira, Luiz C. (Brazil) on 2 Dec 2009, 1:48 p.m.,
in response to message #5 by Peter A. Gebhardt

Keine Problem, mein Freund.

I use AltaVista's Babel Fish Translator as a spell checker, but sometimes knowledge is not enough... So I have my non-English sentences corrected (the ones in English should need advise, though...)

If I could, I'd actually learn as many idioms as possible, because this is one of the main interests of mine since I was about 20 YO: communication with anyone with no language concerns. Did not have the chance then, trying to fulfill this 'need' of mine lately.

And here is one of the best places ever to find people from many places with a common interest and eager for sharing and learning.

Cheers.

Luiz (Brazilie)

PS: Yes, the sentence looks like Portuguese and can be understood.

Edited: 4 Dec 2009, 6:47 a.m.

            
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #7 Posted by Ángel Martin on 2 Dec 2009, 1:42 p.m.,
in response to message #4 by Vieira, Luiz C. (Brazil)

slightly off-topic but pls. bear with me. On the SandMath I added a couple of functions written by Häkan Thörgren that make effective use of the 41C Time Module to create a true random seed, and a series of random numbers based on such.

Cheers, AM

            
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #8 Posted by Peter A. Gebhardt on 8 Dec 2009, 2:22 p.m.,
in response to message #4 by Vieira, Luiz C. (Brazil)

Following up on the sources Luiz already named, James Soules for the HP Business Forums pointed me to the implementation of RAND within the HP-48:

http://forums11.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=1391505

http://www.hpcalc.org/hp48/docs/misc/rand.txt

Quote:
R(n+1) = R(n) * 2851130928467 (mod 10^15)

Best regards,

Peter A. Gebhardt

      
Re: HP17b & it's offsprings - Random Number Generator - How?
Message #9 Posted by Don Shepherd on 2 Dec 2009, 8:14 p.m.,
in response to message #1 by Peter A. Gebhardt

Peter, Luis is right, the 17bii+ does have a clock and the solver has a CTIME function that returns the current time. For instance, at PM 9:13:28, if you enter equation A=CTIME and solve for A, A will be 21.1328, so you could probably use that number in some way to generate a random number rather than prompting the user to enter some number, which is never elegant.

      
RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #10 Posted by Peter A. Gebhardt on 3 Dec 2009, 4:11 a.m.,
in response to message #1 by Peter A. Gebhardt

With a little help from my friends (from this forum) ... :-)

(Example) Pseudorandom Generator for 17b

Uniform Distribution with Seed: RNDU = FP(9821*G(RNDU)+.211327) *) **) ***)
"True" Distribution w/o Seed: RAND = FP(9821*G(RAND)*CTIME+.211327)

Other approaches can be found by looking at

http://en.wikipedia.org/wiki/Random_number_generation

Best regards,

Peter A. Gebhardt

PS(2009, Dec 4th 23:49 MEZ): Or look here for indepth research on potentially useful algorithms

http://en.wikipedia.org/wiki/Linear_congruential_generator

*) Credit for using these values goes to A.C.Rodríguez and his posting here on 11 Aug 2007 conc. 35s seed (Just found it by scanning the Forums Archive PDFs)

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv017.cgi?read=121217

**) and it's original Author Jean-Marc Baillard here:

http://www.hpmuseum.org/software/41/41ranjm.htm

***) And even Microsoft EXCEL did use it - claiming it's good up to a million "draws", but failing statistical tests:

http://support.microsoft.com/kb/86523

Edited: 8 Dec 2009, 5:08 p.m. after one or more responses were posted

            
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #11 Posted by John B. Smitherman on 3 Dec 2009, 6:33 p.m.,
in response to message #10 by Peter A. Gebhardt

Hi Peter. I entered your equations into my yellow / gold 17bii+ (s/n 61801536) and it doesn't seem to work. When I run either equation they return 0.211327. Either I have done something wrong or there is a difference between the 17b and 17bii+. Any ideas?

Regards,

John

                  
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #12 Posted by Peter A. Gebhardt on 3 Dec 2009, 7:45 p.m.,
in response to message #11 by John B. Smitherman

John,

I've tested on a 17bII (s/n: 3610M00859) and a 17bII+ (gold, s/n: CNA42903649).

The only difference: the 17bII when started with seed == 0 in RNDU or RAND respectively, does return 0 at the first key press.

After pressing CALC do a CLR DATA first.

Best regards,

Peter A. Gebhardt

                        
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #13 Posted by Don Shepherd on 3 Dec 2009, 10:41 p.m.,
in response to message #12 by Peter A. Gebhardt

Peter, are you sure you tested on a 17bii+ unit? I got the same results as John, running on both the old 17bii+ (gold) and newer 17bii+ (retro silver). I haven't tested on my 17b or 17bii yet, but I have no doubt it will work on those because their solver works differently (that is, better) than the solver in the plusses. The plus solver has that problem with an extra pass at the beginning of the equation evaluation, and I'd bet that that is the reason your equations don't work, at least on John and my plus units.

Don Shepherd

                              
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #14 Posted by Vieira, Luiz C. (Brazil) on 4 Dec 2009, 6:44 a.m.,
in response to message #13 by Don Shepherd

Hi;

in my HP17BII+ (gold, SN CN33015xxx), after pressing [CALC] the calculator returns [INVALID EQUATION] and the cursor stops at R in G(RAND). I checked the equation many times, added separators in all arithmetic operations, and the error is marked in the same place.

I´ll try with the HP17BII later.

Cheers.

Luiz (Brazil)

                                    
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #15 Posted by Peter A. Gebhardt on 4 Dec 2009, 7:13 a.m.,
in response to message #14 by Vieira, Luiz C. (Brazil)

Luiz,

try to use a different variable name instead of RAND on both sides of the equation. If the error persists then you might have a problem because of the L() & G() issues with the early 17bII+'sses.

Checked s/n definition @Graig Finseth's site ( http://www.finseth.com/hpdata/serial.php ): ccywwnnnnn - your unit is apparently from 2003 and contains an older ROM version which was already reported of having problems in this area. Pls. scan the archives for more information.

There is another obvious problem with the 2nd formula which was pointed out by John already, but somewhat misinterpreted by me:

The uninitialized G(RAND) as long as it evaluates to 0 (zero) returns a fix SEED of .211327 for RAND ...

After you've entered the equations (or after an Error with L() & (G) as encountered here), pls. make sure that you CLR ALL variables while in SOLVE but not in CALC mode (clearing variables for ALL equations!)

BUT BE CAREFUL NOT TO DELETE ALL YOUR EQUATIONS !!!

Best regards,

Peter A. Gebhardt

Edited: 4 Dec 2009, 12:16 p.m.

                                          
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator
Message #16 Posted by John B. Smitherman on 7 Dec 2009, 11:46 a.m.,
in response to message #15 by Peter A. Gebhardt

Thanks Peter. It's still a "no-go" after trying different sequences using the CLR DATA function. I've decided that there must be differences in ROM's that keeps this from working correctly. :-(

Thanks and regards,

John

            
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #17 Posted by Marcus von Cube, Germany on 7 Dec 2009, 12:52 p.m.,
in response to message #10 by Peter A. Gebhardt

Here is a version that works on my 17bii+ (silver):

RNDU = L(SEED:FP(9821*SEED+.211327))
Solve for RNDU! Problem: RCL SEED should return the same value as RCL RNDU but it doesn't. Is the equation evaluated more than once?
                  
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #18 Posted by John B. Smitherman on 7 Dec 2009, 1:58 p.m.,
in response to message #17 by Marcus von Cube, Germany

Thanks Marcus, it works. I agree, I'm not sure what's going on with RCL SEED and RCL RNDU.

Regards,

John

                  
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #19 Posted by Don Shepherd on 7 Dec 2009, 2:34 p.m.,
in response to message #17 by Marcus von Cube, Germany

Quote:
Is the equation evaluated more than once?

Yes, a minimum of two times, and more if it iterates, which it frequently does on the 17bii+'s. See article 830. The solver in the plusses does not work like the solver in the 17b or 17bii. It is illogical, and my attempts to ask HP to clarify exactly how it does work have not been successful. I'm convinced there is no one currently within HP who knows how the plus solver currently works.

                        
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #20 Posted by Marcus von Cube, Germany on 7 Dec 2009, 3:45 p.m.,
in response to message #19 by Don Shepherd

But this still doesn't explain, why SEED and RNDU differ in value. If I set SEED to 0 and solve for RNDU, then RNDU is set to 0.2113... (the last constant in the expression) which I do expect, but SEED is set to a different value: 0,6538. I'm puzzled.

                              
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #21 Posted by Don Shepherd on 7 Dec 2009, 4:18 p.m.,
in response to message #20 by Marcus von Cube, Germany

Marcus, you are right to be puzzled. If anyone at HP can diagnose this and is listening, we'd love to hear from you.

                                    
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #22 Posted by John B. Smitherman on 7 Dec 2009, 4:30 p.m.,
in response to message #21 by Don Shepherd

I tried running the self-test to see if it would report the ROM version at the end of the test but no luck. Are you aware of any sequence of button pushes that would provide this?

John

                                          
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #23 Posted by Don Shepherd on 7 Dec 2009, 5:17 p.m.,
in response to message #22 by John B. Smitherman

John, I just went through by 17bii+ "file" and I could not find any reference to a key sequence that will give you a version number or ROM number. If there is such a sequence, I'm not aware of it.

Don

                                          
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #24 Posted by Peter A. Gebhardt on 8 Dec 2009, 6:33 p.m.,
in response to message #22 by John B. Smitherman

John,

be careful using the selftest! It could clear your calc's memory and all your stored equations.

Look here for a description of keypresses:

http://www.educalc.net/1757088.page

Best regards,

Peter A. Gebhardt

Edited: 9 Dec 2009, 4:02 a.m.

                              
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #25 Posted by Peter A. Gebhardt on 9 Dec 2009, 10:42 a.m.,
in response to message #20 by Marcus von Cube, Germany

Marcus,

Quote:
But this still doesn't explain, why SEED and RNDU differ in value. If I set SEED to 0 and solve for RNDU, then RNDU is set to 0.2113... (the last constant in the expression) which I do expect, but SEED is set to a different value: 0,6538. I'm puzzled.
as expected above findings could not be reproduced on my 17bII+. My results:

Keys      Display
---------------------
0 SEED    SEED=0.0000
RNDU      RNDU=0.2113
RCL SEED  SEED=0.2113
RNDU      RNDU=0.6538
RCL SEED  SEED=0.6538
RNDU      RNDU=0.1212
RCL SEED  SEED=0.1212
...

Best regards,

Peter A. Gebhardt

                                    
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #26 Posted by Marcus von Cube, Germany on 9 Dec 2009, 10:56 a.m.,
in response to message #25 by Peter A. Gebhardt

So you must have a different version of the solver in your machine. Mine is a silver (newer?) release.

Peter, your results look fine. You can simply use the formula and you will get the desired pseudo random sequence on your machine.

                  
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #27 Posted by Peter A. Gebhardt on 8 Dec 2009, 1:36 p.m.,
in response to message #17 by Marcus von Cube, Germany

Marcus,

your solution does work only for a single SEED / RNDU combination. (On my Gold 17bII+)

Pressing RNDU a 2nd time shows a value outside 0 ... 1

Best regards,

Peter A. Gebhardt

                        
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #28 Posted by Marcus von Cube, Germany on 8 Dec 2009, 5:47 p.m.,
in response to message #27 by Peter A. Gebhardt

I tested it with my silver 17bii+ and it looked correct. Can you set SEED to 0 and post the first few RNDU results here?

Keys      Display
---------------------
0 SEED    SEED=0.0000
RNDU      RNDU=0.2113
RCL SEED  SEED=0.6538
RNDU      RNDU=0.1222
RCL SEED  SEED=0.3473
RNDU      RNDU=0.5160
RCL SEED  SEED=0.1911

Marcus

                              
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #29 Posted by Peter A. Gebhardt on 8 Dec 2009, 6:16 p.m.,
in response to message #28 by Marcus von Cube, Germany

Marcus,

Now I understand - you are using a 2-step process.

What I'm after is, storing the 'seed' (or even none) into RNDU once and then proceed (by only pressing RNDU again) to get the next value in sequence - and so on ...

Keys      Display
---------------------
0 RNDU    
RNDU=0.2113
RNDU=0.6538
RNDU=0.1222
RNDU=0.3473
RNDU=0.5160
...

That's the way the formula in Message #10 works on both units of mine. It does even work, if you use an initial RNDU outside { 0 ... 1 }

Best regards,

Peter A. Gebhardt

Edited: 8 Dec 2009, 6:19 p.m.

                                    
Re: RNDU & RAND for HP17b & it's offsprings - Random Number Generator - 17bii+ Version
Message #30 Posted by Marcus von Cube, Germany on 9 Dec 2009, 2:18 a.m.,
in response to message #29 by Peter A. Gebhardt

This is how it *should* work, but it doesn't do on the 17bii+. I tried to overcome the situation by adding the seed variable. Normally, the modified sequence should be identical to the original formula if you only set SEED once and press RNDU multiple times. Because of reasons yet to determine, this isn't the case on the "+". The formula gets evaluated twice with each step but returns the RNDU result of the first step. Therefore, SEED (checked with RCL SEED) and RNDU differ. If you can live with the fact that the formula returns every other result of the pseudo random sequence, just press RNDU in each step.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall