Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL random number

Status
Not open for further replies.

shellghost

Programmer
Sep 11, 2002
2
US
Does anyone know of a way to get a random number (or close to it) using AS/400 COBOL? I'm looking for a way to randomly choose 25 people from a file and that seems to be the way to go.
 
Doesn't have your cobol flavor the intrinsic RANDOM function ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Does AS/400 support intrinsic functions? If not there are COBOL PRNGs out there you can use.
 
In the absence of the RANDOM function, you can always use the milliseconds from the system time function you may have to obtain a "random" number or at least a random seed to work with.
 
Hi
I worte some algorithm when i started to write COBOL and that was a translation from an assembler program when i study that at the university.
Code:
000001 IDENTIFICATION DIVISION.
000002 PROGRAM-ID. RAND.
000003 ENVIRONMENT DIVISION.
000004 DATA DIVISION.
000005 WORKING-STORAGE SECTION.
000006 01  CURRENT-X.
000007     02  CURRENT-TIME PIC X(6).
000008     02  CURRENT-DATE PIC X(6).
000009 01  CURRENT-Y REDEFINES CURRENT-X.
000010      02  CURR01          PIC X.
000011      02  CURR02          PIC X.
000012      02  CURR03          PIC X.
000013      02  CURR04          PIC X.
000014      02  CURR05          PIC X.
000015      02  CURR06          PIC X.
000016      02  CURR07          PIC X.
000017      02  CURR08          PIC X.
000018      02  CURR09          PIC X.
000019      02  CURR10          PIC X.
000020      02  CURR11          PIC X.
000021      02  CURR12          PIC X.
000022 01  CURRENT-9 PIC 9(9) VALUE 0.
000023 01  CURRENT-R REDEFINES CURRENT-9.
000024      02  RAND-NUMBER     PIC 9999.
000025      02  FILLER          PIC X.
000026      02  CURR06          PIC X.
000027      02  CURR04          PIC X.
000028      02  CURR12          PIC X.
000029      02  CURR05          PIC X.
000030****************************************
000031 PROCEDURE DIVISION.
000032 0.  ACCEPT CURRENT-TIME FROM TIME.
000033     ACCEPT CURRENT-DATE FROM DATE.
000034     MOVE CORR CURRENT-Y TO CURRENT-R.
000035     MULTIPLY 65535 BY CURRENT-9.
000036     DISPLAY RAND-NUMBER.

That will output random number between 1-9999.
If you want other range:
Code:
DIVIDE RAND-NUMBER BY N GIVING I REMAINDER REM.
ADD 1 TO REM.
Barry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top