RPGLE Generate Random Numbers - Using CEERAN0 API

ILE RPG has no native random number generator. An ILE-bindable API, CEERAN0, can be invoked from ILE RPG programs to generate such numbers.

CEERAN0 takes as input an integer seed, which it updates each time to avoid repetitions in the random number sequence, and outputs a double precision random number (if given 0 as a seed, CEERAN0 will generate a random number based on the current system time).

The program sample in this document illustrates how to produce random numbers.

RPGLE Sample Source Code for Random Number Generation

H DFTACTGRP(*NO) ACTGRP('QILE')                         
d CEERAN0         PR                                    
d seed                          10I 0                   
d ranno                          8F                     
d fc                            12A   options(*omit)    
d highno          s             10I 0 Inz(9000000)      
d lowno           s             10I 0 Inz(1000000)      
d seed            s             10I 0 inz(0)            
d rand            s              8F                     
d range           s             10I 0                   
d result          s             10I 0                   
     range = (highno - lowno) + 1;                      
     CEERAN0( seed : rand : *omit );                    
     result = %int(rand * range) + lowno;               
     dsply result;                                      

Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.