Post Reply 
HP 12C Random Integer Number Generator
11-19-2017, 09:23 PM (This post was last modified: 11-19-2017 09:41 PM by Dieter.)
Post: #3
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HP 12C Random Integer Number Generator - Dieter - 11-19-2017 09:23 PM



User(s) browsing this thread: 1 Guest(s)