Generating a Random number on an iSeries in CL
Page 1 of 1
Generating a Random number on an iSeries in CL
Neither CLP nor RPG has a Pseudo Random Number generator built in, so we have to kludge it to get it to work. You can link the QLEMF C library and use the CERNAN0 api .
The other way is to use the QSH shell to do it. But this method generates a random number between 0 - 32767 inside the QSH shell env and then stores the result in a DTAARA so that the calling program can access the value, as there in no native way to return vars from QSH environment
Firt creare a dataarea
CRTDTAAARA MyRand *CHAR 5
Then use the following CL source to get the random from Qshell
PGM
DCL &CMD *CHAR 40
DCL &RANDNO *CHAR 5
CHGVAR VAR(&CMD) VALUE('print $RANDOM|datarea -lw MYRAND')
QSH CMD(&CMD)
RTVDTAARA DTAARA(MYRAND) RTNVAR(&RANDNO)
ENDPGM
The other way is to use the QSH shell to do it. But this method generates a random number between 0 - 32767 inside the QSH shell env and then stores the result in a DTAARA so that the calling program can access the value, as there in no native way to return vars from QSH environment
Firt creare a dataarea
CRTDTAAARA MyRand *CHAR 5
Then use the following CL source to get the random from Qshell
PGM
DCL &CMD *CHAR 40
DCL &RANDNO *CHAR 5
CHGVAR VAR(&CMD) VALUE('print $RANDOM|datarea -lw MYRAND')
QSH CMD(&CMD)
RTVDTAARA DTAARA(MYRAND) RTNVAR(&RANDNO)
ENDPGM
kasthuri- Posts : 212
Join date : 2013-01-27
Similar topics
» How to generate the Random number
» No of number records in a PF
» Have OS/400 notify you when a file contains more than a certain number of deleted records
» Have OS/400 notify you when a file contains more than a certain number of deleted records?
» No of number records in a PF
» Have OS/400 notify you when a file contains more than a certain number of deleted records
» Have OS/400 notify you when a file contains more than a certain number of deleted records?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|