星期二, 10月 31, 2023

2000-10-25 如何於RPGIV中使用C函數產生亂數



□ Tips : 如何於RPGIV中使用C函數產生亂數


---------------------------------------
範例:利用C函數 SRAND 與 RAND 產生亂數

QRPGLESRC  RANDR

      * ------------------------------------------------------------- *

      * Description| Program to Randomly generate a 4 digit *

      * | number and dump the results. *

      * | This is a prototype only. *

      * ============================================================= *

      * WARNING ! ! ! SPECIFIC COMPILATION ! ! ! WARNING *

      * To compile this program properly you will have to use the *

      * following option : *

      * CRTBNDRPG DFTACTGRP(*NO) *

      * BNDDIR(QC2LE) *

      * This is to allow the called function 'rand' to be retrieved *

      * from a non-RPG program library. *

     H DEBUG  OPTION(*SRCSTMT:*NODEBUGIO)
     H DFTACTGRP(*NO)


     D SeedRandom      PR            10I 0 ExtProc('srand')

     D SeedP                         10U 0 Value

     D GetRandom       PR            10I 0 ExtProc('rand')

      * Regular numeric variables to receive result of random function.

     D #Num1           S              5P 0

     D #Num2           S              5P 0

     D #Num3           S              5P 0

     D #Num4           S              5P 0

      * LongTime has a "Z" datatype, the 20 byte timestamp.

     D TimeStampZ      S               Z

      * From Program Status Data Structure

      * Job Number, to be used to make a unique number in the data structure.

     D MYPSDS         SDS
     D  JOB_NUM              264    269S 0

      * Alpha Time Stamp from the TIME Instruction in the "Z" type var.

     D DSTimeStamp     DS
     D  ASMil                 21     26S 0
     D  AlphaTime              1     26A

     **************************************************************************

      *
      * New Random with Time & Jobnumber combined as a seed.
      *

     C                   TIME                    TimeStampZ
     C                   MOVE      TimeStampZ    AlphaTime
     C                   TIME                    DATTIM            6 0
      *
      * Regular GetRandom with rand function
     C                   CallP     SeedRandom(JOB_NUM + DATTIM + ASMil)

     C                   EVAL      #num1 = GetRandom
     C                   EVAL      #num2 = GetRandom
     C                   EVAL      #num3 = GetRandom
     C                   EVAL      #num4 = GetRandom
     **************************************************************************
      *
     C                   DUMP
     C                   MOVE      *ON           *INLR



參考資料:
Interfacing RPG IV with the C language high-level math library

Converting from C prototypes to RPG prototypes callc.html 

"C Library Ref." SC09-2119

"ILE C/400 Programmer’s Ref." SC09-2070

"System API Ref. OS/400 UNIX-type APIs" SC41-4875

"MI Functional Ref." SC41-4810

"ILE C/400 Programmer’s Ref." SC09-2070

"ILE C/C++ MI Library Ref." V3R7

"OS/400 Sockets Programming V3R6" SC41-4422 (TCP/IP)

"CPA Toolkit/400 Ref." SC41-4802
      


沒有留言: