Using C to randomize

Processing a multiple occurrence data structure.

This program uses C modules listed in the QC2LE binding directory to generate random, or in this case semi-random numbers, since the first three digits of the number actually represent the day of the year. The time of day is used to seed the random number, the returned value is tested to determine if it has already been used. Binding to the C directory gives RPG some interesting functions to augment RPG op codes.

    H OPTION(*SRCSTMT : *NODEBUGIO) DFTACTGRP(*NO) ACTGRP('QILE')
     H BNDDIR('SOFTCODE':'QC2LE')

      ****************************************************************
      * PROGRAM NAME - ADM050RP                                      *
      *                                                              *
      * FUNCTION     - This program is bound to QC2LE to use the     *
      *                random number generator.                      *
      *                                                              *
      ****************************************************************
 
     FADMHDRPF  IF   E           K DISK

    ‚ *---
    ‚ /copy qrpglesrc,swcmmn_pr
    ‚ *---

     D ADM050RP        PR
     D OrderNbr                       8s 0

     D ADM050RP        PI
     D OrderNbr                       8s 0

     D c0Seed          S              5i 0  INZ(0)

     D GetRandomNbr    PR            10i 0 Extproc('rand')

     D SetRandom       PR                  ExtProc('srand')
     D  Seed                         10U 0 Value

     D randomNbr       S             10i 0
     D Today           S              8s 0
     D TimeOfDay       S              6s 0
     D DayNbr          S              3s 0

     D NewOrderNumber...
     D                 S               n

     **-- Random Number Conversion: ---------------------------------- **
     D MaxNbr          S             10i 0  INZ(000099999)
     D MinNbr          S             10i 0  INZ(000001000)
     **
  
	NewOrderNumber = *off;
	DOU NewOrderNumber;
	   DoU randomNbr >= MinNbr AND randomNbr <= MaxNbr;
	      Today = DateToday();
	      DayNbr = DayOfYear();
	      TimeOfDay = TimeNow();
	      SetRandom(Today + TimeOfDay);
              randomNbr = GetRandomNbr();
	   ENDDO;
	   OrderNbr = (DayNbr * 100000) + randomNbr;
	   CHAIN OrderNbr ADMHDRPF;
	   IF not %found;
	      NewOrderNumber = *on;
	   ENDIF;
	ENDDO;
	RETURN;