Post Reply 
HP 12C Random Integer Number Generator
11-20-2017, 01:45 PM (This post was last modified: 11-20-2017 02:13 PM by Dieter.)
Post: #5
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
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-20-2017 01:45 PM



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