I have long given consideration to generating random numbers using RPG. "Why?" you ask. "Just for the sport of exercising my mind," I respond.
Then, as things happen, I was asked to produce random passwords for my organization. My immediate thought was, which I was smart enough to not repeat out loud, "You mean I get to play on company time. I love this job."
The project really was fun and took less than a day to complete the analysis and write the program. The rules to be applied were a password could be anywhere from 6 to 10 characters long and must contain a single digit in any position except the first.
I came up with and used two interesting concepts to make the random thing really happen.
The first concept began with the assumption that anything random, at least where computers are involved must start with numbers and counters. Continuing, if two separate counters are used where the first is used to terminate the second, randomness can be obtained. The problem is that the controlling counter cannot be under influence of the program incrementing the second counter. Introducing time. Time is essentially a counter and is controlled by the system not the program. RPG provides us with a TIME operand, which populates a variable. When time is retrieved there is know way of knowing how far into the current second the system is thus giving a very random amount of time for execution of a DO LOOP. Activity on the system will also effect how many times the loop is executed.
In my program I used the TIME operand to retrieve the current time. This gives the program up to one full second t
To continue reading for free, register below or login
To read more you must become a member of Search400.com
');
// -->

o execute the counter loop. As can be seen in the example program the system time is retrieved prior to executing the counter loop and saved. Then time is retrieved with each increment of the counter and compared to the saved time. When time and save time are not equal the counter loop terminates.
The second concept was to code the secondary counter to take into account any specific rules that apply thus limiting the results at termination to only valid numbers. Doing so eliminates having to do any non-random calculations to obtain valid output.
In the example program where password length is calculated, note that password length is initialized to 5 so the first increment will cause it to be 6. In the loop if the counter exceeds 10, the counter is reset to 6. During execution of the DO LOOP the counter is always within the confines of the rules at the ENDDO statement. Care was taken to ensure the counter was initialized at onset to a number that after the first increment the number would be valid result.
Below are a few examples of the output from my program 'TJRRPG' where RPG stands for Random Password Generator.
KN0ZBT
XLLQKD3GAB
UFMJQS0BTL
SW6NJNG
TKS7RSE
VPO5XWK
RSG8VIG
UPW2MZTDTG
NE2JPW
PEXI3NJ
DTWFHD9TNW
SKVRC3WZJX
BEBV7HUHS
TB0CEG
QTI7GQVFWS
NT1NUYI
E8BKLNEPOE
KSRA1X
WSY2YWFMXO
DIVD2R
DW6WXUT
WV2PHQ
PFYCD3F
NSLH9AYTQN
UBA9LJN
EACKG5VV
FTR3KCMCH
JRE3QEO
OHNBQX1
XVO4BEC
FOCDNX3F
OYAGSZ0
Code