RPGLE Generate Random Number using rand(), srand() as well as clock() C funcions

Each fourth dimension you lot telephone squall upwards rand(), it volition hand you lot a number. They’ll await similar random numbers, but actually it’s merely the upshot of a complicated mathematical formula. The work alongside this is if you lot run your programme together with it gives you lot 53, 1143, 543, 9853 hence the side yesteryear side fourth dimension you lot run your program, it volition hand you lot the exact same sequence of numbers. That’s annoying.

So what you lot create is called “seeding the random release generator“. That’s basically merely telling it “okay, get-go from here”. If each fourth dimension you lot order it to get-go from a unlike number, hence the sequence volition hold upwards unlike each fourth dimension you lot run your program.

So how create you lot larn a release that’s going to hold upwards unique each fourth dimension you lot run your program? There are diverse possibilities, but the virtually obvious is the appointment together with time. Obviously, side yesteryear side fourth dimension you lot run your program, it volition hold upwards a unlike fourth dimension of day, or a unlike day, or both.

So if you lot role srand(time()) to seed the random release generator alongside the electrical flow time, you’ll larn a unlike sequence of “random” numbers each fourth dimension you lot run your program.

RPGLE Sample Code to Generate Random Number

h option(*nodebugio) dftactgrp(*no) bnddir('QC2LE')                      * Prototype to C "rand" business office                                       d Rand            PR            10I 0 ExtProc('rand')                                                                                            * Prototype to C "srand" business office                                      d SRand           PR                  ExtProc('srand')                  d   iSeed                       10U 0 VALUE                                                                                                      * Prototype to C "clock" business office                                      d GetTime         PR            10I 0 ExtProc('clock')                                                                                           * Global variables                                                     d RandomNumber    due south             10I 0                                   d ClockTicks      due south             10I 0                                   d SeedValue       due south             10U 0                                                                                                            /free                                                                                                                                               ClockTicks = GetTime;                                                   SeedValue = ClockTicks;                                                 SRand(SeedValue);                                                       RandomNumber = Rand();                                                  dsply RandomNumber;                                                                                     *inlr = *on;                                                /end-free       

Recommended Reading

Hosting Unlimited Indonesia


Sumber http://www.mysamplecode.com/