Home > AS/400 Tips > iSeries programmer tips > Generate random numbers using RPG
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Generate random numbers using RPG


James Greene
07.22.2002
Rating: -2.32- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
iSeries CL programming
Checking in on your IBM i authorization lists
Running PHP open source applications: NOBODY needs authority
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
CL program for daily backups
An automated CL method of moving a query from AS/400 to Excel
Changing user password expiration
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help
How do I retrieve the source for an output queue description to put in to a CL program?

iSeries programmer tips
Groovy programming on IBM i
EGL Rich UI on IBM i: Do you Dojo?
Running PHP open source applications: NOBODY needs authority
Programming for the Web on the IBM i, what is possible
Using geocoding on AS/400 to enhance your Web presence
The best technologies and tools for System i programmers in 2009
Seven IBM i project lessons learned in 2008
Documenting nested program structures on the AS/400
What is an integrated database?
An automated CL method of moving a query from AS/400 to Excel

Application Development
iSeries calling an .exe
Top 10 programmer tips
Formatted work job scheduler
Convert system date and time
Mixing free format code with embedded SQL
SQL update a field in one file from a field in another file
Webcasts for iSeries programmers
Programming advice from the pros
Easy code copying via the drag and drop method
Setting FTP time-outs

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


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




Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts