HP 12C Random Integer Number Generator
11-16-2017, 04:58 AM (This post was last modified: 11-16-2017 06:06 AM by Gamo.)
Post: #1
 Gamo Senior Member Posts: 712 Joined: Dec 2016
HP 12C Random Integer Number Generator
This program is to generate 6 digits integers for quick pick lottery generator using HP 12C
Using program to calculates uniformly distributed pseudo-random numbers from the HP 12 CP Solution Handbook and added the integer digits that I want.

To use this program: [R/S] then GTO18 [R/S]

Code:
 . (decimal) 5284163 STO 0 997 x FRAC STO 0 R/S GTO 10 999999 (6 Digits) x 1 + INTG GTO 00

Example:
[R/S] result 0.83 [GTO 18] result 831,051
[R/S] result 0.27 [GTO 18] result 272,860
[R/S] result 0.20 [GTO 18] result 196,339
[R/S] result 0.75 [GTO 18] result 750,082

Is there any better or shorter solution for this kind of random program?

Thank You

Gamo
11-16-2017, 08:06 AM
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 12C Random Integer Number Generator
(11-16-2017 04:58 AM)Gamo Wrote:  Is there any better or shorter solution for this kind of random program?

This generator r:=frac(997*r) was quite popular in the Seventies as it was fast and short and provided good results. However, some care about the initial seed was required, so some implementations (including yours) set this seed within the program. Which means that the produced number sequence is not random, but the same each time you restart the program.

Another well-reputed RNG that was often used in HP programs is r:=frac(9821*r+0,211327). This one is less picky about the initial seed. Simply enter anything between 0 and 1 in R0 and run the program.

For many purposes even simpler generators may do. For instance r:=frac[(r+pi)²]. I often used this one in games programs and I had no problems with the results.

There was an extensive thread on RNGs last year, related to an translation of HP67 games for the HP41. You will find more information there, including articles in HP-related papers, discussing different generators. The one mentioned above turned out to be a good choice.

Dieter
11-19-2017, 09:23 PM (This post was last modified: 11-19-2017 09:41 PM by Dieter.)
Post: #3
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 12C Random Integer Number Generator
(11-16-2017 04:58 AM)Gamo Wrote:  Is there any better or shorter solution for this kind of random program?

Gamo, I just noticed that your program ends with a GTO 00. This means that after a random integer has been generated the program will restart at the beginning and so it will produce the same number again... #-)

I would probably do it some way like this:

Code:
01  INTG 02  STO 3 03  LN          // generate error if n < 1 04  , 05  2 06  1 07  1 08  3 09  2 10  7 11  STO 2 12  9 13  8 14  2 15  1 16  STO 1 17  0 18  RCL 0 19  x<=y? 20  CHS 21  FRAC 22  STO 0       // make sure 0 =< seed < 1 23  RCL 0 24  RCL 1 25  * 26  RCL 2 27  + 28  FRAC 29  STO 0 30  RCL 0       // leave 0 =< r < 1 on stack 31  RCL 3 32  * 33  INTG 34  1 35  +           // return 1 =< r_int =< n 36  R/S 37  GTO 23

Initialize: store a random seed between 0 and 1 in R0.

Enter a maximum positive integer n and start the program with f[PRGM] [R/S].
The result is a pseudo-random integer between 1 and n. Press [R/S] for more numbers.
If a pseudo random number between 0 and 1 is required press [X<>Y].

Example:
0,4711 [STO] 0
999 f[PRGM]
[R/S] => 884
[R/S] => 169
[R/S] => 919

The PRNG used here is able to produce one million different results, so n should not be larger than this.

Dieter
11-20-2017, 04:46 AM (This post was last modified: 11-22-2017 06:08 AM by Gamo.)
Post: #4
 Gamo Senior Member Posts: 712 Joined: Dec 2016
RE: HP 12C Random Integer Number Generator
On 11C and 15C both have dedicated RAN# function that very easy to program random 6 digits number using RAN#

Program:

Code:
 LBL E RAN# 999999 x 1 + INT RTN

To generate 6 digits random number f USER then press E as many time as you like.

Gamo
11-20-2017, 01:45 PM (This post was last modified: 11-20-2017 02:13 PM by Dieter.)
Post: #5
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 12C Random Integer Number Generator
(11-20-2017 04:46 AM)Gamo Wrote:  On 11C and 15C both have dedicated RAN# function that very easy to program random 6 digits number using RAN#

With a built-in PRNG of course things are easy. However, there is a numeric pitfall. Since the RAN# function may return results as large as 0,9999999999 (i.e. 1–10–10) the intermediate result of r · n may round to n and the final result then is n+1.

Example:
Suppose you want random numbers between1 and 2000.
0,9999999999 · 2000 + 1 = 2001,000000

Or simply think of generating numbers between 1 and 3, e.g. for a game:
0,9999999999 · 3 + 1 = 4,000000000

This can be avoided by removing the final digit of the random number (and, just to be sure, adding 1 after the INT truncation):

Code:
RAN# EEX 9 x INT EEX 9 / 2000 x INT 1 +

...or, faster and shorter, simply...

Code:
RAN# 1 + FRAC 2000 x INT 1 +

Anyway, the mentioned problem does not occur for your special case n = 999999, so you can leave your program as it is. ;-)
Also the well known and simple PNRG r := frac[(r+pi)²] is not affected as it generates numbers with (mostly) 8 and (sometimes) 9 decimals on a 10-digit calculator.

Dieter
11-21-2017, 05:13 AM
Post: #6
 Gamo Senior Member Posts: 712 Joined: Dec 2016
RE: HP 12C Random Integer Number Generator
Thank You Dieter

Very useful information about Random Number Generator

Gamo
11-04-2018, 04:03 PM
Post: #7
 SlideRule Senior Member Posts: 1,324 Joined: Dec 2013
RE: HP 12C Random Integer Number Generator
(11-16-2017 04:58 AM)Gamo Wrote:  … program is to generate 6 digits integers for quick pick lottery generator using HP 12C … calculates uniformly distributed pseudo-random numbers … added the integer digits that I want.
Is there any better or shorter solution for this kind of random program?
Gamo

It took a little time to research, but this routine for the Sharp PC-1211 from the October 1981 edition of Your Computer (pg.54) matches your range requirement of integers from one to six.
[attachment=6553]
BEST!
SlideRule
11-04-2018, 06:02 PM (This post was last modified: 11-04-2018 06:04 PM by Dieter.)
Post: #8
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 12C Random Integer Number Generator
(11-04-2018 04:03 PM)SlideRule Wrote:  It took a little time to research, but this routine for the Sharp PC-1211 from the October 1981 edition of Your Computer (pg.54) matches your range requirement of integers from one to six.

The relevant part here is not the range of the random integers but the random number generator itself (whose results are then scaled to 1...6). If I understand this correctly, the method is

r := frac[ (r+77) · pi5 ]

Here pi5 is 306,0196848 so that before the FRC command the intermediate result is between 23563,5... and 23869,5..., which in turn means that on a 10-digit calculator the resulting random number (between 0 and 1) has merely five significant digits.

Can someone say something about other properties of this generator? How does it compare to the simple and fast frac[(r+pi)²] generator?

Dieter
 « Next Oldest | Next Newest »