IBM Support

Generating Random Numbers in ILE RPG IV Using the CEERAN0 API

Troubleshooting


Problem

ILE RPG has no native random number generator. An ILE-bindable API, CEERAN0, can be invoked from ILE RPG IV 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).

Resolving The Problem

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 and store them into an output file.
DDS Source Code for Seed File
 
     A          R SEEDREC
     A            SEEDFLD       10B 0   


DDS Source Code for Random Number File
 
     A          R RANDREC
     A            RANDFLD       17F 7       FLTPCN(*DOUBLE)

Note: You need to add a record to the file SEEDPF. If you do not add a record, you will end up with message RNX1221 when you try to run the application. For example, add a record where SEEDFLD has a 0 (zero).
Use SQL to insert the record. For example
INSERT INTO <your schema>/SEEDPF  VALUES(0);  
ILE RPG Source Code named RNDNBR2
**FREE
//
// This file only contains one record, i.e. the seed value for the random number generator API,
// CEERAN0.The seed which CEERAN0 returns each time has to be kept and fed back to the API on
// the next invocation: this is needed in order to avoid repeatedly generating the same random
// number.
//
Dcl-F seedpf     Usage(*Update:*Delete) USROPN;
//
// This file contains the random numbers generated by the API.
//
Dcl-F randpf     Usage(*Output) USROPN;
//
// Number of random numbers to be generated during one run.
//
Dcl-C Times           const(1000);
//
// Correction factor (see additional related comments in C specs).
//
Dcl-C Factor          const(10000000000);
//
// Definitions for standard data types.
//
Dcl-S StdPacked       Packed(15:5);                                      // Packed
Dcl-S StdInt          Int(10);                                           // Integer
Dcl-S StdDouble       Float(8);                                          // Double Precision
Dcl-S StdFC           Char(12);                                          // Feedback Code
//
// Prototype for random number generator API.
//
Dcl-Pr randnum_gen extproc('CEERAN0');
  Pr_seed         LIKE(StdInt);
  Pr_randnum      LIKE(StdDouble);
  Pr_FC           LIKE(StdFC);
End-Pr;
//
//
// Counter variable for looping.
//
Dcl-S counter         LIKE(StdInt);
//
// Variable to contain current seed value.
//
Dcl-S seednum         LIKE(StdInt);
//
// Variable to contain current random number.
//
Dcl-S randnum         LIKE(StdDouble);
//
// Variable to contain API's feedback code.
//
Dcl-S feedbackcode    LIKE(StdFC);
//
//
//
// Open input and output files.
//
Open seedpf;
Open randpf;
//
// Read last seed value from previous run.
//
Read seedrec;
//
// Set first seed value of this run to last seed value generated in the program's previous
// run.  The field "seedfld" is defined via DDS; when imported by the RPG compiler, the field
// comes in as packed(20,0) and has to be scaled by 10**10 before assigning it into seednum,
// which is integer(10,0).
//
seednum = %rem(seedfld:Factor);
//
// Loop for the number of times previously specified.
//
For counter = 1 by 1 to Times;
  //
  // Generate random number, update seed value.
  //
  randnum_gen(seednum:randnum:feedbackcode);
  //
  // Append new random number to the end of the output file.
  //
  randfld = randnum;
  Write randrec;
EndFor;
//
// Set seed for next program run to last seed returned from the API.
//
seedfld = seednum;
Update seedrec;
//
// Close input and output files.
//
Close randpf;
Close seedpf;
*inlr = *on;       
Compile using CRTBNDRPG   PGM(<yourlib>/RNDNBR2)   SRCFILE(<yourlib>/QRPGLESRC)   SRCMBR(RNDNBR2) DFTACTGRP(*NO)
              
CALL <yourlib>/RNDNBR2

Check the result using SQL
select * from <your schema>.randpf
image-20241113134003-1
 

[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m3p000000hB4rAAE","label":"API"},{"code":"a8m0z0000000CHtAAM","label":"Programming ILE Languages"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

22835203

Document Information

Modified date:
13 November 2024

UID

nas8N1017440