How to generate random numbers in cobol

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

How to generate random numbers in cobol

hello friends,
i am new to mainframes app programming
can any one put your ideas on how to generate random nos in cobol
kirangentlebreeze

Posts: 9
Joined: Wed Oct 27, 2010 6:39 pm
Has thanked: 0 time
Been thanked: 0 time

Re: how to generate random numbers in cobol

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'.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator

Posts: 3004
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: how to generate random numbers in cobol

Hello and welcome to the forum,

Once determined, how will a random number be used?
Hope this helps,
d.sch.

dick scherrer
Global moderator

Posts: 6269
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: how to generate random numbers in cobol

i am just a beginner in mainframes
my application needs to generate a 16 digit random no which should be unique
kirangentlebreeze

Posts: 9
Joined: Wed Oct 27, 2010 6:39 pm
Has thanked: 0 time
Been thanked: 0 time

Re: how to generate random numbers in cobol

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
Robert Sample
Global moderator

Posts: 3652
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 269 times

Re: how to generate random numbers in cobol

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!
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator

Posts: 2927
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 154 times

Re: how to generate random numbers in cobol

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.
Hope this helps,
d.sch.

dick scherrer
Global moderator

Posts: 6269
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times