|
There is often a need to generate a random within a specific range
of numbers.
As is often the case with random number generators, the random
number detailed herein requires an initial random number to start the process. In the code
below this is entered in theparameter &EXPO and can be loaded using any number such as
current date and/or time. Since the parameter is a return variable, its new value can be
used on subsequent calls to the generator.
The parameter &BASE indicates the range within which the random
number should be generated. For example a &BASE value of 64 will generate a random
number in the range 1 to 64.
/* ********************************************************/
/* * This program generates a random number in the range 1-BASE * */
/* * EXPO is a random exponent used to force field overflow. * */
/* * * */
/* * Author: Trevor D. Seeney, SENTINEX Inc. * */
/*********************************************************/
PGM PARM(&RANDOM# &EXPO &BASE)
/* * Input Parameters * */
DCL VAR(&RANDOM#) TYPE(*DEC) LEN(2 0)
DCL VAR(&EXPO) TYPE(*DEC) LEN(6 0)
DCL VAR(&BASE) TYPE(*DEC) LEN(2 0)
/* * Working variables * */
DCL VAR(&WORK) TYPE(*DEC) LEN(6 0)
DCL VAR(&DIV) TYPE(*DEC) LEN(6 0)
/* * Standard Error Handling Declaratives * */
DCL VAR(&MSGID) TYPE(*CHAR) LEN(7)
DCL VAR(&MSG) TYPE(*CHAR) LEN(200)
DCL VAR(&MSGDTA) TYPE(*CHAR) LEN(200)
DCL VAR(&MSGF) TYPE(*CHAR) LEN(10)
DCL VAR(&MSGL) TYPE(*CHAR) LEN(10)
MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ERROR))
MONMSG MSGID(MCH1210)
CHGVAR VAR(&DIV) VALUE(999999 / &BASE + 1)
CHGVAR VAR(&EXPO) VALUE(&EXPO + 521347)
CHGVAR VAR(&RANDOM#) VALUE((&EXPO / &DIV) + 1)
CHGVAR VAR(&EXPO) VALUE(&EXPO + ((&BASE + 1) - +
&RANDOM#))
RETURN
/* * Standard Error Handling * */
ERROR:
MSGD: RCVMSG MSGTYPE(*DIAG) MSG(&MSG) MSGDTA(&MSGDTA) +
MSGID(&MSGID) MSGF(&MSGF) MSGFLIB(&MSGL)
IF COND(&MSGID *NE ' ') THEN(DO)
SNDPGMMSG MSGID(&MSGID) MSGF(&MSGL/&MSGF) +
MSGDTA(&MSGDTA) MSGTYPE(*DIAG)
GOTO CMDLBL(MSGD)
ENDDO
RCVMSG MSGTYPE(*EXCP) MSG(&MSG) MSGDTA(&MSGDTA) +
MSGID(&MSGID) MSGF(&MSGF) MSGFLIB(&MSGL)
IF COND(&MSGID *NE ' ') THEN(SNDPGMMSG +
MSGID(&MSGID) MSGF(&MSGL/&MSGF) +
MSGDTA(&MSGDTA) MSGTYPE(*ESCAPE))
ENDPGM
|