Generate random numbers using RPG

Need to generate random passwords? Learn how using these two concepts.

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 to 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


    100      f*  written by James H. Greene                                                                             07/15/02

    200      f*  email -    TRIPLEJ46@hotmail.com                                                                       07/17/02

    300      f*  home  -    920-757-5089                                                                                07/17/02

    400      f*  work  -    920-751-7169                                                                                07/17/02

    500      f*  Program ID        -  TJRRPG                                                                            07/15/02

    600      f*  Date Written      -  07/10/2002                                                                        07/10/02

    700      f*  Purpose           -  return 6 to 10 character password with                                            07/10/02

    800      f*                       one digit imbedded                                                                07/10/02

    900      f**********************************************************                                                07/10/02

   1000      d pwlength        s              2  0                                                                      07/10/02

   1100      d digitpos        s              2  0                                                                      07/10/02

   1200      d digit           s              1  0                                                                      07/17/02

   1300      d x               s              2  0                                                                      07/10/02

   1400      d y               s              2  0                                                                      07/10/02

   1500      d timea           s              6  0                                                                      07/10/02

   1600      d timeas          s              6  0                                                                      07/10/02

   1700      d password        s             10                                                                         07/10/02

   1800      d digita          s              1                                                                         07/10/02

   1900      d alphabet        c                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                         07/17/02

   2000      c*****************************************************************                                         07/10/02

   2100      c*  get length of password                                                                                 07/18/02

   2200      c                   time                    timea                                                          07/10/02

   2300      c                   eval      timeas = timea                                                               07/18/02

   2400      c                   eval      pwlength = 5                                                                 07/10/02

   2500      c                   dou       timea <> timeas                                                              07/18/02

   2600      c                   time                    timea                                                          07/10/02

   2700      c                   eval      pwlength = pwlength + 1                                                      07/10/02

   2800      c                   if        pwlength > 10                                                                07/18/02

   2900      c                   eval      pwlength = 6                                                                 07/18/02

   3000      c                   endif                                                                                  07/17/02

   3100      c                   enddo                                                                                  07/10/02

   3200      c*  do for length of password - fill password 1 char at a time                                             07/18/02

   3300      c                   dou       x = pwlength                                                                 07/17/02

   3400      c                   eval      x = x + 1                                                                    07/10/02

   3500      c                   time                    timea                                                          07/10/02

   3600      c                   eval      timeas = timea                                                               07/18/02

   3700      c*  find y where y will = 1 thru 26 for position in alphabet                                               07/17/02

   3800      c                   dou       timea <> timeas                                                              07/18/02

   3900      c                   time                    timea                                                          07/10/02

   4000      c                   eval      y = y + 1                                                                    07/10/02

   4100      c*  if outside limits then start counting again - 26 char in alphabet                                      07/17/02

   4200      c                   if        y > 26                                                                       07/10/02

   4300      c                   eval      y = 1                                                                        07/18/02

   4400      c                   endif                                                                                  07/10/02

   4500      c                   enddo                                                                                  07/10/02

   4600      c*  populate the password with character data                                                              07/18/02

   4700      c                   eval      %subst(password:x:1) =                                                       07/17/02

   4800      c                             %subst(alphabet:y:1)                                                         07/10/02

   4900      c                   enddo                                                                                  07/10/02

   5000      c*  get digit to be inserted into password                                                                 07/18/02

   5100      c                   eval      digit = -1                                                                   07/18/02

   5200      c                   time                    timea                                                          07/10/02

   5300      c                   eval      timeas = timea                                                               07/18/02


  5769PW1 V4R4M0  990521                  SEU SOURCE LISTING                            07/18/02 14:57:12                 PAGE    2

  SOURCE FILE . . . . . . .  SYSADMIN/QRPGLESRC 
  MEMBER  . . . . . . . . .  TJRRPG 

  SEQNBR*...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 ...+... 9 ...+... 0 
   5400      c                   dou       timea <> timeas                                                              07/10/02

   5500      c                   time                    timea                                                          07/10/02

   5600      c                   if        digit >= 9                                                                   07/18/02

   5700      c                   eval      digit = -1                                                                   07/18/02

   5800      c                   endif                                                                                  07/17/02

   5900      c                   eval      digit = digit + 1                                                            07/10/02

   6000      c                   enddo                                                                                  07/10/02

   6100      c                   move      digit         digita                                                         07/10/02

   6200      c*  get position of digit in password -must be > 1 & <= password length                                    07/18/02

   6300      c                   eval      digitpos = 1                                                                 07/17/02

   6400      c                   time                    timea                                                          07/10/02

   6500      c                   eval      timeas = timea                                                               07/18/02

   6600      c                   dou       timea <> timeas                                                              07/18/02

   6700      c                   time                    timea                                                          07/10/02

   6800      c                   eval      digitpos = digitpos + 1                                                      07/10/02

   6900      c                   if        digitpos > pwlength                                                          07/17/02

   7000      c                   eval      digitpos = 2                                                                 07/18/02

   7100      c                   endif                                                                                  07/10/02

   7200      c                   enddo                                                                                  07/10/02

   7300      c*  place digit into  password                                                                             07/18/02

   7400      c                   eval      %subst(password:digitpos:1) = digita                                         07/10/02

   7500      c*  display password                                                                                       07/18/02

   7600      c     password      dsply                                                                                  07/10/02

   7700      c                   eval      *inlr = *on                                                                  07/10/02


                                  * * * *  E N D  O F  S O U R C E  * * * * 
This was first published in July 2002

Dig deeper on iSeries CL programming

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchEnterpriseLinux

SearchDataCenter

Close