Random number generator -- and calling C functions from RPG

Ever get frustrated that you can't do certain things in RPG? Well you might like to check out the ILE C run-time library functions. Don't worry! You don't have to learn C and you don't even need a C compiler!

The ILE C library functions provide lots of useful utilities unavailable in RPG, such as complex math and random number generation. These functions are implemented in a set of service programs with names starting QC2..., e.g. QC2UTIL1 etc. These exist on your AS/400 even if you don't have C installed.

The program code below shows how to call these functions. The program will return a 4-byte integer containing a pseudo-random number between 1 and 32767. If passed a negative number, the random number generator will "re-seed" itself, i.e. start a new sequence of pseudo-random numbers.

To create the program, you need to compile the RPG code below as a module using the CRTRPGMOD command, then create the program binding in the C service program using:


 		* Prototype to C "rand" function                         
D GenRandNbr      PR            10I 0 ExtProc('rand')     
 * Prototype to C "srand" function                        
D SeedRand        PR                  ExtProc('srand')    
D   iuSeed                      10U 0 VALUE               
 * Prototype to C "clock" function                        
D GetTime         PR            10I 0 ExtProc('clock')    
 * Constants                                              
D TRUE            C                   '1'                 
D FALSE           C                   '1'                        
 * Global variables                                              
D RandomNumber    S             10I 0                            
D FirstTime       S              1A   INZ(TRUE)                  
D ClockTicks      S             10I 0                            
D SeedValue       S             10U 0                            
 * Program parameters                                            
C     *ENTRY        PLIST                                        
C                   PARM                    RandomNumber         
 * First time only or if parameter is negative                   
C                   IF           FirstTime = TRUE                 
C                             or RandomNumber < 0                 
 * Get clock "ticks" - if fails return error indicator            
C                   EVAL      ClockTicks = GetTime                
C                   IF        ClockTicks < 0                      
C                   EVAL      RandomNumber = -1                   
C                   RETURN                                        
C                   ENDIF                                         
 * Seed the random number generator                               
C                   EVAL      SeedValue = ClockTicks              
C                   CALLP     SeedRand(SeedValue)                 
C                   EVAL      FirstTime = FALSE               
C                   ENDIF                                     
 * Generate a random number                                   
C                   EVAL      RandomNumber = GenRandNbr       
C                   RETURN    

This was first published in May 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

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.