Microsoft KB Archive/61672

How to Generate Random Numbers in COBOL 3.00 and 3.00a

PSS ID Number: Q61672 Article last modified on 05-16-1990

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: This article contains a sample COBOL program that generates a random integer between 1 and some desired integer. This information applies to Microsoft COBOL versions 3.00 and 3.00a for MS-DOS and MS OS/2.

More Information: An adaptation of the linear-congruential method is used in this program to generate a random integer within a desired range. The following formula is used in the program shown below: Result = (Seed * 214013 + 2531011) MOD (MaxInt) “Result” refers to the random integer that is generated each time through the main loop in the program below. “Seed” is the value that is altered each time through the loop in order to generate a new random number. On the first time through the loop, Seed gets its value from the hundredths of a second on the system clock. “MOD” refers to the mathematics operation “modulus,” which divides the two numbers and returns the remainder instead of the quotient. “MaxInt” is the upper bound of how many possible different integers the program will generate. The program generates integers that are between 1 and MaxInt. This linear-congruential formula is similar to that used by RND in Microsoft BASIC. For information on how the linear-congruential formula can be used in a Microsoft BASIC program to generate random numbers, query on the following words: RND and RANDOMIZE and alternatives and generating and random and numbers

Example
$SET ANS85 DATA DIVISION. WORKING-STORAGE SECTION. 01 TIME-IN PIC 9(8). 01 TODAY-TIME REDEFINES TIME-IN. 05 DUMMY-HOUR         PIC 99. 05 DUMMY-MINUTES PIC 99. 05 DUMMY-SECONDS PIC 99. 05 HUNDREDTHS         PIC 99. 77 NUMBER1       PIC 999999  VALUE 214013. 77 NUMBER2       PIC 9999999 VALUE 2531011. 77 UPPERBOUND         PIC 9         VALUE 6. 01 SEED          PIC 9(11). 01 TEMP          PIC 9(11). 01 GARBAGE       PIC 9(11). 01 THE-RESULT         PIC 9(11). 01 THE-TABLE occurs 6 times pic 9999 VALUE 0. 01 I             PIC 99 VALUE 0. PROCEDURE DIVISION. DISPLAY &quot;Generating Random Numbers... (Please Wait)&quot;. *Get the original seed by using the time. ACCEPT TIME-IN FROM TIME. MOVE HUNDREDTHS TO SEED. PERFORM 10000 TIMES *Generate a random number called temp COMPUTE TEMP = SEED * NUMBER1 + NUMBER2 *Use the random number stored in temp to create a random *integer between 1 and UPPERBOUND. DIVIDE TEMP BY UPPERBOUND GIVING GARBAGE REMAINDER THE-RESULT ADD 1 TO THE-RESULT *Reseed for the next time through the loop. MOVE TEMP TO SEED *Keep track of the random numbers which were created. ADD 1 TO THE-TABLE(THE-RESULT). *Print out the number of times each number was generated. PERFORM 6 TIMES ADD 1 TO i     DISPLAY i; &quot; occurred &quot;; THE-TABLE(I); &quot; times.&quot;. Copyright Microsoft Corporation 1990.