Page 1 of 1

How to generate random numbers in cobol

PostPosted: Wed Oct 27, 2010 6:42 pm
by kirangentlebreeze
hello friends,
i am new to mainframes app programming
can any one put your ideas on how to generate random nos in cobol

Re: how to generate random numbers in cobol

PostPosted: Wed Oct 27, 2010 6:48 pm
by NicC
First check to see if there is already a BIF (Built-In Function) in COBOL - I cannot remember if there is or not, then look at the functions provided by LE (Language Environment) - it probably has. Failing that, look for an algorithm on the internet: first thing I would try for this option is google 'random number algorithm'.

Re: how to generate random numbers in cobol

PostPosted: Thu Oct 28, 2010 1:21 am
by dick scherrer
Hello and welcome to the forum,

Once determined, how will a random number be used?

Re: how to generate random numbers in cobol

PostPosted: Thu Oct 28, 2010 6:49 pm
by kirangentlebreeze
i am just a beginner in mainframes
my application needs to generate a 16 digit random no which should be unique

Re: how to generate random numbers in cobol

PostPosted: Thu Oct 28, 2010 6:58 pm
by Robert Sample
Assuming you're using Enterprise COBOL, you can use FUNCTION RANDOM to generate a sequence of "random" numbers. The COBOL Language Reference manual gives details. http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/FINDBOOK?filter=enterprise+cobol&SUBMIT=Find

Re: how to generate random numbers in cobol

PostPosted: Thu Oct 28, 2010 8:26 pm
by enrico-sorichetti
i am just a beginner in mainframes
my application needs to generate a 16 digit random no which should be unique


even if they are called random the right term should be PSEUDO RANDOM
randomness is a statistical property quite different from uniqueness which is deterministic

when testing the first call to the random number generator has a seed which is used to guarantee
that the sequence of random numbers will be the same across different runs in order to test with
consistent data
when the application is <promoted> to production the programs will have to be modified
in order to not provide the seed with the hope that each run will give a sequence of PSEUDO RANDOM numbers
( different from the previous ones )

if the main requirement is uniqueness it would be advisable to review the approach!

Re: how to generate random numbers in cobol

PostPosted: Thu Oct 28, 2010 11:56 pm
by dick scherrer
Hello,

IDENTIFICATION DIVISION.                                   
PROGRAM-ID. EXT7.                                           
DATA DIVISION.                                             
WORKING-STORAGE SECTION.                                   
01 MIN-NUMBER PIC 99 VALUE 10.                             
01 MAX-NUMBER PIC 99 VALUE 20.                             
01 RANDOM-NUMBER PIC 99.                                   
PROCEDURE DIVISION.                                         
MAIN-PARA.                                                 
     PERFORM 10 TIMES                                       
         COMPUTE RANDOM-NUMBER = FUNCTION RANDOM *         
                            (MAX-NUMBER - MIN-NUMBER + 1) +
                             MIN-NUMBER                     
         DISPLAY 'RANDOM NUMBER:' RANDOM-NUMBER             
     END-PERFORM.                                           
     STOP RUN.