Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cobol random number generator

1,953 views
Skip to first unread message

Guy

unread,
Jul 11, 2003, 1:51:41 PM7/11/03
to
Does anyone have a random number generator for Cobol that they would
be willing to share?

Stephen Fuld

unread,
Jul 11, 2003, 2:53:06 PM7/11/03
to

"Guy" <guy.s...@unisys.com> wrote in message
news:a1344e9a.03071...@posting.google.com...

> Does anyone have a random number generator for Cobol that they would
> be willing to share?

I presume you mean pseudo random number generator.

It would be trivial in that it is a single line of COBOL to do a linear
congruential PRNG (pseudo random number generator). The key is the
particular values and the sizes of the fields. So you need to specify what
the maximum value, or equivalently the maximum number of times you will call
it before you are willing to have the sequence repeat. It also depends upon
for which architecture you are talking about as the constants are different
depending upon how many bits there are before multiplicative overflow
occurs, so you need to say whether you are talking A series or 2200 series.

An other alternative, which takes advantage of code you that is around is to
use the DES encryption subroutine. Just start with a seed, feed it through
the encryption to get a new value, then feed that value back through the
encryption to get the next one. Be aware that this is much slower than the
kind I mentioned in the previous paragraph, but if you are only going to
call it a few times, it may not matter.

This is a deep subject and not to be taken lightly. Don't try to "roll your
own" without some expert advice from a book or a knowledgeable person. But
doing it right isn't all that hard.

--
- Stephen Fuld
e-mail address disguised to prevent spam


le...@ber-hi-tag-there-nolli.net

unread,
Jul 11, 2003, 3:20:55 PM7/11/03
to
Stephen Fuld <s.f...@pleaseremove.att.net> wrote:

IAWTP.

Someone (Larry Wall?) said that the generation of random numbers is far too
important to be left to chance.

With regards to your request, however, I do not have any code to share.
Sorry.

--Lee

Chuck Stevens

unread,
Jul 11, 2003, 7:21:23 PM7/11/03
to
"Stephen Fuld" <s.f...@PleaseRemove.att.net> wrote in message news:<mIDPa.50534$3o3.3...@bgtnsc05-news.ops.worldnet.att.net>...

>
> I presume you mean pseudo random number generator.
>
> It would be trivial in that it is a single line of COBOL to do a linear
> congruential PRNG (pseudo random number generator). The key is the
> particular values and the sizes of the fields. So you need to specify what
> the maximum value, or equivalently the maximum number of times you will call
> it before you are willing to have the sequence repeat. It also depends upon
> for which architecture you are talking about as the constants are different
> depending upon how many bits there are before multiplicative overflow
> occurs, so you need to say whether you are talking A series or 2200 series.

For all but one among the four cases I can think of, that's a long way
around the barn.

If the dialect of COBOL you're talking about, regardless of platform,
is '85-compliant, the standard mandates the presence of FUNCTION
RANDOM. The optional argument is the seed, which must be zero or a
positive integer if provided. The result is greater than or equal to
zero and less than one.

On MCP/AS systems, from any COBOL dialect, a pseudo-random-number
generator can also be accessed: entry point RANDOM in GENERALSUPPORT.
Example 2 in the chapter on LIBRARIES in the COBOL74 reference manual
illustrates exactly such a mechanism.

That leaves '74-and-prior compilers on 2200 systems, and I can't help
you there.

-Chuck Stevens

Edward Reid

unread,
Jul 11, 2003, 10:23:30 PM7/11/03
to
On Fri, 11 Jul 2003 13:51:41 -0400, Guy wrote

> Does anyone have a random number generator for Cobol that they would
> be willing to share?

Sure. It's called COBOL ANSI-85. Use the FUNCTION RANDOM intrinsic.

If you're not using COBOL85, it's time to upgrade.

As others have noted, you can get into serious trouble trying to do it
yourself.

On Fri, 11 Jul 2003 14:53:06 -0400, Stephen Fuld wrote


> I presume you mean pseudo random number generator.

If you want to get picky, what he really wants is a generator of a a
sequence of pseudo-independent pseudo-random numbers with a specified
(probably uniform) distribution. [Knuth, p2.] There is no such thing as
a random number, only a random distribution of a sequence of numbers.

I agree that you should not roll your own. [Knuth, p4-5] gives a
complicated method that he once created in an attempt to give a truly
random-looking sequence. It degenerated almost immediately. (Although
Knuth tells the story on himself, I doubt that he designed the method
for any reason other than to reach the moral he did. He is too good a
mathematician to have believed it would work.)

On Fri, 11 Jul 2003 15:20:55 -0400, le...@ber-hi-tag-there-nolli.net
wrote


> Someone (Larry Wall?) said that the generation of random numbers is far too
> important to be left to chance.

I think you're thinking of Donald Knuth. [Knuth, p5] says "The moral of
this story is that random numbers should not be generated with a method
chosen at random. Some theory should be used." He doesn't give a
reference, and he's usually very good about giving credit where it's
due. So I suspect that Knuth said it first, though others have
undoubtedly made the same point.

As I said, using the COBOL85 RANDOM intrinsic is by far the best and
easiest. If forced to do your own, get a copy of [Knuth].

Edward Reid

Reference: [Knuth] _The Art of Computer Programming_ (2nd ed), Volume
2: Seminumerical Algorithms, by Donald E. Knuth, 1981. (1st ed was
1969. This is one of the classics of computing, and Don Knuth is one of
the true heroes of computing, obscured today by the mere promoters.)


Randall Bart

unread,
Jul 12, 2003, 2:11:47 PM7/12/03
to
'Twas Fri, 11 Jul 2003 22:23:30 -0400 when all comp.sys.unisys stood in
awe as Edward Reid <edw...@paleo.org> uttered:

>I think you're thinking of Donald Knuth. [Knuth, p5] says "The moral of
>this story is that random numbers should not be generated with a method
>chosen at random. Some theory should be used."

Sure thing. I remember the Apple ][ had poor random number routines. In
floating point Basic, the random function would fall into ruts. You would
find it cycling through some short list of numbers. Apple Integer Basic
had a more subtle oddity which I discovered.

My friend Rick Schwall PhD had an Apple computer which he had rigged to a
coinbox for kids to play games at a store. He repeatedly heard claims
that the random number generator wasn't random. He ran all kinds of
tests, and every test he devised came up less than 1% away from a perfect
distribution, which is good enough for a little 16 bit integer function.
Then one day I was just fooling around writing a program which wrote
random pixels to the screen. I noticed a pattern in the randomness, and
started working on exaggerating the pattern. I eventually had a program
like this:

10 GR
20 XLIM=32
30 YLIM=32
40 X=RND(XLIM)
50 IF X>40 GOTO 40
60 COLOR=RND(16)
70 Y=RND(YLIM)
80 IF Y>40 GOTO 40
90 PLOT X,Y
100 GOTO 40

Note: This is reproduced from memory over twenty years old, and may
contain minor errors.
Note: This is an integer random function. The parameter is the size of
the range. RND(x) produces a number in the set [0:x-1] The seed is not
readily visible to the programmer, but could be read with PEEK
instructions and set with POKE instructions.

It would appear that this program just puts random spots of color at
random spots on the screen. Yet the image that appears has monocolored
rectangles like this

00112233445566770011223344556677
00112233445566770011223344556677
00112233445566770011223344556677
00112233445566770011223344556677
00112233445566770011223344556677
00112233445566770011223344556677
00112233445566770011223344556677
00112233445566770011223344556677
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF
8899AABBCCDDEEFF8899AABBCCDDEEFF

If you alter XLIM and YLIM (that's why they're variables) you see
different results. If you make one of them an odd number, the results
look random. The more factors of two in XLIM and YLIM, the more regular
the results. If you set XLIM and YLIM to 128, most points on the screen
are never hit, with some monocolored rectangles at regular intervals.

It's easy to deduce how the RND function worked. On each call it applied
some manipulation to the seed then return that seed MOD the parameter.
But the manipulation took some group of bits (at least five by my
research) and shifted them over unmodified. It produced a good
distribution, but if you had consecutive calls involving multiple factors
of 2, the function was seriously flawed.

Rick wrote this up for a magazine and used my name, but I never saw the
article.
--
RB |\ © Randall Bart
aa |/ ad...@RandallBart.spam.com Bart...@att.spam.net
nr |\ Please reply without spam I LOVE YOU 1-917-715-0831
dt ||\ http://RandallBart.com/ DOT-HS-808-065 MS^7=6/28/107
a |/ Play http://sky.4r.st Ånåheim Ångels 2002 World Chåmps!
l |\ This is why we spend billions of dollars for drug research:
l |/ http://www.cbc.ca/webone/alison/

Marc Wilson

unread,
Jul 15, 2003, 9:34:41 AM7/15/03
to comp.sys.unisys
In comp.sys.unisys, Edward Reid <edw...@paleo.org> (Edward Reid) wrote in
<01HW.BB34E9620...@news-east.usenetserver.com>::

|On Fri, 11 Jul 2003 13:51:41 -0400, Guy wrote
|> Does anyone have a random number generator for Cobol that they would
|> be willing to share?
|
|Sure. It's called COBOL ANSI-85. Use the FUNCTION RANDOM intrinsic.

|. [Knuth, p5] says "The moral of

|this story is that random numbers should not be generated with a method
|chosen at random. Some theory should be used." He doesn't give a
|reference, and he's usually very good about giving credit where it's
|due. So I suspect that Knuth said it first, though others have
|undoubtedly made the same point.
|
|As I said, using the COBOL85 RANDOM intrinsic is by far the best and
|easiest. If forced to do your own, get a copy of [Knuth].

It depends on how random it needs to be. For some applications, just
getting the last 2 digits of the time in milliseconds is good enough.
--
Marc Wilson
___________________________________________________________________
Cleopatra Consultants Limited - IT Consultants - CoolICE Partner - MAPPER Associate
Tel: (44/0) 70-500-15051 Fax: (44/0) 870-164-0054
Mail: in...@cleopatra.co.uk Web: http://www.cleopatra.co.uk
___________________________________________________________________
MAPPER User Group mailing list: send *SUBSCRIBE to M...@cleopatra.co.uk

Guy

unread,
Jul 16, 2003, 3:27:42 PM7/16/03
to
In my case we are wanting to provide a random number
from a set of numbers. For example a random number between 1 and 500.
And for clarification, this is an MCP environment. I looked at the library
example, but don't see how we can restrict this to a set of numbers.

Chuck Stevens

unread,
Jul 16, 2003, 4:03:54 PM7/16/03
to
GENERALSUPPORT.RANDOM produces a floating-point pseudorandom value greater
than or equal to zero and less than one. The same is true for COBOL85's
FUNCTION RANDOM. Producing a pseudorandom value from 1 to 500 is a matter
of arithmetic using the result from either. You don't indicate whether
the limits are inclusive or exclusive (or a combination of both). But
consider:

If I took the library example from the COBOL74 reference manual, and added
011000 77 FINAL-NUM PIC 9(11) BINARY.
026100 COMPUTE FINAL-NUM = 500 * (1 + RANDOM-RESULT).
027500 COMPUTE FINAL-NUM ROUNDED = 500 * (1 + RANDOM-RESULT)
I'd expect FINAL-NUM to contain a value of at least one and no more than 499
at Sequence 026200, and a value of at least one and no more than 500 at
Sequence 028000.

If the range is indeterminate until execution time, a pattern like
COMPUTE FINAL-NUM = UPPER-LIMIT * (LOWER-LIMIT + RANDOM-RESULT).
ought to work

In COBOL85, you don't need the library call at all;
COMPUTE FINAL-NUM = UPPER-LIMIT * (LOWER-LIMIT + FUNCTION RANDOM).
is indicative of what should suffice.

Am I missing something here?

-Chuck Stevens

"Guy" <guy.s...@unisys.com> wrote in message
news:a1344e9a.03071...@posting.google.com...

Stephen Fuld

unread,
Jul 17, 2003, 11:39:29 AM7/17/03
to

"Chuck Stevens" <charles...@unisys.com> wrote in message
news:bf4b3b$2m6f$1...@si05.rsvl.unisys.com...

> GENERALSUPPORT.RANDOM produces a floating-point pseudorandom value greater
> than or equal to zero and less than one. The same is true for COBOL85's
> FUNCTION RANDOM. Producing a pseudorandom value from 1 to 500 is a matter
> of arithmetic using the result from either. You don't indicate whether
> the limits are inclusive or exclusive (or a combination of both). But
> consider:
>
> If I took the library example from the COBOL74 reference manual, and added
> 011000 77 FINAL-NUM PIC 9(11) BINARY.
> 026100 COMPUTE FINAL-NUM = 500 * (1 + RANDOM-RESULT).
> 027500 COMPUTE FINAL-NUM ROUNDED = 500 * (1 + RANDOM-RESULT)
> I'd expect FINAL-NUM to contain a value of at least one and no more than
499
> at Sequence 026200, and a value of at least one and no more than 500 at
> Sequence 028000.
>
> If the range is indeterminate until execution time, a pattern like
> COMPUTE FINAL-NUM = UPPER-LIMIT * (LOWER-LIMIT + RANDOM-RESULT).
> ought to work
>
> In COBOL85, you don't need the library call at all;
> COMPUTE FINAL-NUM = UPPER-LIMIT * (LOWER-LIMIT + FUNCTION RANDOM).
> is indicative of what should suffice.
>
> Am I missing something here?

I don't know MCP at all, but taking what you said about how Random works at
face value, your examples are incorrect. Your example at line 26100 will
produce a value between 500 and 1,000. The sum of 1 plus Random Result will
be between 1 and 2, which is then multiplied by 500 to give the limits 500
to 1000. Assuming Random Result is >= zero and < 1, then, to get a range
>=0 and <= 500, simply multiply RAndom Result by 501. If you want to
exclude zero, multiply random result by 500 then add one to the result.

Chuck Stevens

unread,
Jul 17, 2003, 12:07:34 PM7/17/03
to
Thanks for the corrections; I agree with most of what you wrote. The
exceptions:

<<Your example at line 26100 will produce a value between 500 and 1,000.
The sum of 1 plus Random Result will
be between 1 and 2, which is then multiplied by 500 to give the limits 500
to 1000. >>

Actually, RandomResult could be zero but could not be 1. Thus, the final
result from sequence 26100 would be at least 500 and no more than 999.

The point I was trying to emphasize is really not the specifics of the
algorithm but the fact that getting a pseudorandom number within a given
range is easily doable, through system-supplied software available to
COBOL74 and to language-mandated constructs in COBOL85.

-Chuck Stevens


Stephen Fuld

unread,
Jul 17, 2003, 1:07:39 PM7/17/03
to

"Chuck Stevens" <charles...@unisys.com> wrote in message
news:bf6hk6$17ki$1...@si05.rsvl.unisys.com...

> Thanks for the corrections; I agree with most of what you wrote. The
> exceptions:
>
> <<Your example at line 26100 will produce a value between 500 and 1,000.
> The sum of 1 plus Random Result will
> be between 1 and 2, which is then multiplied by 500 to give the limits 500
> to 1000. >>
>
> Actually, RandomResult could be zero but could not be 1. Thus, the final
> result from sequence 26100 would be at least 500 and no more than 999.

Right. Sorry about that.

> The point I was trying to emphasize is really not the specifics of the
> algorithm but the fact that getting a pseudorandom number within a given
> range is easily doable, through system-supplied software available to
> COBOL74 and to language-mandated constructs in COBOL85.

Yes, but, as both of us have illustrated, you have to be carefull to get it
right! :-(

Edward Reid

unread,
Jul 17, 2003, 6:20:00 PM7/17/03
to
On Wed, 16 Jul 2003 16:03:54 -0400, Chuck Stevens wrote

> Am I missing something here?

I don't think so. This is second nature to those of us who have worked
with random number generators for years, or have a background in math,
or both. The explanation you gave is basic, but it's exactly what's
needed for someone coming from a business background and no experience
with random number generators.

Except, as already pointed out, for the details of the example. To get
a number in the half-open range from LOWER-LIMIT (inclusive) to
UPPER-LIMIT (exclusive):

COMPUTE FINAL-NUM =
(UPPER-LIMIT - LOWER-LIMIT) * FUNCTION RANDOM + LOWER-LIMIT

Also, you have to be careful with ROUNDED. If you write

COMPUTE FINAL-NUM ROUNDED = 500 * RANDOM-RESULT + 1

then the resulting distribution will not be uniform -- 1 and 501 will
occur with half the frequency of the integers in between. It's easier
to get it right if you let COBOL truncate it as usual.

Edward


dens...@gmail.com

unread,
Feb 20, 2015, 8:01:20 PM2/20/15
to
On Friday, July 11, 2003 at 12:51:41 PM UTC-5, Guy wrote:
> Does anyone have a random number generator for Cobol that they would
> be willing to share?

Here is an sample of some old code I wrote long ago.

IDENTIFICATION DIVISION.
PROGRAM-ID. RANDGEN as "ConsoleApplication2.RANDGEN".
AUTHOR. Myron D Denson.
DATE-COMPILED.
* **************************************************************
* SUBROUTINE TO GENERATE RANDOM NUMBERS THAT ARE GREATER THAN
* ZERO AND LESS OR EQUAL TO THE RANDOM NUMBERS NEEDED WITH NO
* DUPLICATIONS. (CALL "RANDGEN" USING RANDGEN-AREA.)
*
* CALLING PROGRAM MUST HAVE A COMPARABLE LINKAGE SECTION
* AND SET 3 VARIABLES PRIOR TO THE FIRST CALL IN RANDGEN-AREA
*
* FORMULA CYCLES THROUGH EVERY NUMBER OF 2X2 ONLY ONCE.
* RANDOM-NUMBERS FROM 1 TO RANDOM-NUMBERS-NEEDED ARE CREATED
* AND PASSED BACK TO YOU.
*
* RULES TO USE RANDGEN:
*
* RANDOM-NUMBERS-NEEDED > ZERO
* *** MUST BE LESS THAN 199,983,184 ***
* COUNT-OF-ACCESSES MUST = ZERO FIRST TIME CALLED.
*
* RANDOM-NUMBER = ZERO, WILL BUILD A SEED FOR YOU
* WHEN COUNT-OF-ACCESSES IS ALSO = 0
*
* RANDOM-NUMBER NOT = ZERO, WILL BE NEXT SEED FOR RANDGEN
* (RANDOM-NUMBER MUST BE <= RANDOM-NUMBERS-NEEDED)
*
* YOU CAN PASS RANDGEN YOUR OWN RANDOM-NUMBER SEED
* THE FIRST TIME YOU USE RANDGEN.
*
* BY PLACING A NUMBER IN RANDOM-NUMBER FIELD
* THAT FOLLOWES THESE SIMPLE RULES:
* IF COUNT-OF-ACCESSES = ZERO AND
* RANDOM-NUMBER > ZERO AND
* RANDOM-NUMBER <= RANDOM-NUMBERS-NEEDED
*
* YOU CAN LET RANDGEN BUILD A SEED FOR YOU
*
* THAT FOLLOWES THESE SIMPLE RULES:
* IF COUNT-OF-ACCESSES = ZERO AND
* RANDOM-NUMBER = ZERO AND
* RANDOM-NUMBER-NEEDED > ZERO
*
* TO INSURING A DIFFERENT PATTERN OF RANDOM NUMBERS
* A LOW-RANGE AND HIGH-RANGE IS USED TO BUILD
* RANDOM NUMBERS.
* A LOW-RANGE = 16807
* A HIGH-RANGE = RANDOM-NUMBERS-NEEDED + 16807
* AFTER RANDOM-NUMBER-BUILT IS CREATED
* AND IS BETWEEN LOW AND HIGH RANGE
* RANDUM-NUMBER = RANDOM-NUMBER-BUILT - 16807
*
* **************************************************************
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WORK-AREA.
05 2X2 PIC 9(09) VALUE 2 COMP-3.
05 RANDOM-NUMBER-BUILT PIC 9(09) COMP.
05 FIRST-PART PIC 9(09) COMP.
05 WORKING-NUMBER PIC 9(09) COMP.
05 LOW-RANGE PIC 9(09).
05 HIGH-RANGE PIC 9(09).
05 YOU-PROVIDE-SEED PIC X VALUE SPACE.
05 PAUSE-FOR-A-SECOND PIC X VALUE SPACE.
01 SEED-TIME.
05 HOURS PIC 99.
05 MINUTES PIC 99.
05 SECONDS PIC 99.
05 MS pic 99.
*
* LINKAGE SECTION.
* Not used during testing
01 RANDGEN-AREA.
05 COUNT-OF-ACCESSES PIC 9(09) VALUE ZERO.
05 RANDOM-NUMBERS-NEEDED PIC 9(09) VALUE ZERO.
05 RANDOM-NUMBER PIC 9(09) VALUE ZERO.
05 RANDOM-MSG PIC X(60) VALUE SPACE.
*
* PROCEDURE DIVISION USING RANDGEN-AREA.
* Not used during testing
*
PROCEDURE DIVISION.
100-RANDGEN-EDIT-HOUSEKEEPING.
MOVE SPACE TO RANDOM-MSG.
IF RANDOM-NUMBERS-NEEDED = ZERO
DISPLAY 'RANDOM-NUMBERS-NEEDED ' NO ADVANCING
ACCEPT RANDOM-NUMBERS-NEEDED.
IF RANDOM-NUMBERS-NEEDED NOT NUMERIC
MOVE 'RANDOM-NUMBERS-NEEDED NOT NUMERIC' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF RANDOM-NUMBERS-NEEDED = ZERO
MOVE 'RANDOM-NUMBERS-NEEDED = ZERO' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF RANDOM-NUMBERS-NEEDED > 199983184
MOVE 'RANDOM-NUMBERS-NEEDED > 199983184 MAX' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF COUNT-OF-ACCESSES NOT NUMERIC
MOVE 'COUNT-OF-ACCESSES NOT NUMERIC' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF COUNT-OF-ACCESSES GREATER THAN RANDOM-NUMBERS-NEEDED
MOVE 'COUNT-OF-ACCESSES > THAT RANDOM-NUMBERS-NEEDED'
TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF YOU-PROVIDE-SEED = SPACE AND RANDOM-NUMBER = ZERO
DISPLAY 'DO YOU WANT TO PROVIDE SEED Y OR N: '
NO ADVANCING
ACCEPT YOU-PROVIDE-SEED.
IF RANDOM-NUMBER = ZERO AND
(YOU-PROVIDE-SEED = 'Y' OR 'y')
DISPLAY 'ENTER SEED ' NO ADVANCING
ACCEPT RANDOM-NUMBER.
IF RANDOM-NUMBER NOT NUMERIC
MOVE 'RANDOM-NUMBER NOT NUMERIC' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF RANDOM-NUMBER GREATER THAN RANDOM-NUMBERS-NEEDED
MOVE 'RANDOM-NUMBER > THAT RANDOM-NUMBER-NEEDED'
TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
200-RANDGEN-DATA-HOUSEKEEPING.
COMPUTE RANDOM-NUMBER-BUILT = RANDOM-NUMBER + 16807.
COMPUTE HIGH-RANGE = RANDOM-NUMBERS-NEEDED + 16807.
MOVE 2 TO 2X2.
MOVE 16807 TO LOW-RANGE.
300-SET-2X2-DIVISOR.
IF 2X2 GREATER THAN HIGH-RANGE
NEXT SENTENCE
ELSE
COMPUTE 2X2 = 2X2 * 2
GO TO 300-SET-2X2-DIVISOR.
400-RANDGEN-START.
IF COUNT-OF-ACCESSES = ZERO AND RANDOM-NUMBER = ZERO
* *********************************************************
* IF FIRST TIME THROUGH AND YOU WANT TO BUILD A SEED. *
* *********************************************************
MOVE FUNCTION CURRENT-DATE (9:8) TO SEED-TIME
DISPLAY 'SEED-TIME ' SEED-TIME
COMPUTE RANDOM-NUMBER-BUILT =
((SECONDS * HOURS * MINUTES * MS) + HIGH-RANGE) - 3600.
* *********************************************
* END OF BUILDING A SEED IF YOU WANTED TO *
* *********************************************
* ***************************************************
* THIS PROCESS IS WHERE THE RANDOM-NUMBER IS BUILT *
* ***************************************************
500-RANDGEN-FORMULA.
COMPUTE FIRST-PART = (5 * RANDOM-NUMBER-BUILT) + 7.
DIVIDE FIRST-PART BY 2X2 GIVING WORKING-NUMBER
REMAINDER RANDOM-NUMBER-BUILT.
600-RANDGEN-PROCESS-CHECK.
IF RANDOM-NUMBER-BUILT GREATER THAN LOW-RANGE AND
RANDOM-NUMBER-BUILT LESS THAN (HIGH-RANGE + 1)
GO TO 700-RANDGEN-CLEANUP.
GO TO 500-RANDGEN-FORMULA.
* *********************************************
* GOOD RANDOM NUMBER HAS BEEN BUILT *
* *********************************************
700-RANDGEN-CLEANUP.
ADD 1 TO COUNT-OF-ACCESSES.
COMPUTE RANDOM-NUMBER =
RANDOM-NUMBER-BUILT - 16807.
DISPLAY 'RANDOM NUMBER ' RANDOM-NUMBER.
IF COUNT-OF-ACCESSES < RANDOM-NUMBERS-NEEDED
GO TO 100-RANDGEN-EDIT-HOUSEKEEPING.
900-EXIT-RANDGEN.
DISPLAY 'RANDOM-MSG:' RANDOM-MSG.
ACCEPT PAUSE-FOR-A-SECOND.
GOBACK.

dens...@gmail.com

unread,
Feb 22, 2015, 2:00:12 AM2/22/15
to
On Friday, July 11, 2003 at 12:51:41 PM UTC-5, Guy wrote:
> Does anyone have a random number generator for Cobol that they would
> be willing to share?

I updated the code to fix some after midnight problems and other problems. Sorry for the first set of code. But have not found a way to delete it from forum.

IDENTIFICATION DIVISION.
PROGRAM-ID. RANDGEN as "ConsoleApplication2.RANDGEN".
AUTHOR. Myron D Denson.
DATE-COMPILED.
* **************************************************************
* SUBROUTINE TO GENERATE RANDOM NUMBERS THAT ARE GREATER THAN
* ZERO AND LESS OR EQUAL TO THE RANDOM NUMBERS NEEDED WITH NO
* DUPLICATIONS. (CALL "RANDGEN" USING RANDGEN-AREA.)
*
* CALLING PROGRAM MUST HAVE A COMPARABLE LINKAGE SECTION
* AND SET 3 VARIABLES PRIOR TO THE FIRST CALL IN RANDGEN-AREA
*
* FORMULA CYCLES THROUGH EVERY NUMBER OF 2X2 ONLY ONCE.
* RANDOM-NUMBERS FROM 1 TO RANDOM-NUMBERS-NEEDED ARE CREATED
* AND PASSED BACK TO YOU.
*
* RULES TO USE RANDGEN:
*
* RANDOM-NUMBERS-NEEDED > ZERO
*
* COMPUTE LOW-RANGE =
* ((SECONDS * HOURS * MINUTES * MS) / 3).
* A HIGH-RANGE = RANDOM-NUMBERS-NEEDED + LOW-RANGE
* AFTER RANDOM-NUMBER-BUILT IS CREATED
* AND IS BETWEEN LOW AND HIGH RANGE
* RANDUM-NUMBER = RANDOM-NUMBER-BUILT - LOW-RANGE
*
* **************************************************************
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WORK-AREA.
05 X2-POWER PIC 9 VALUE 2.
05 2X2 PIC 9(12) VALUE 2 COMP-3.
05 RANDOM-NUMBER-BUILT PIC 9(12) COMP.
05 FIRST-PART PIC 9(12) COMP.
05 WORKING-NUMBER PIC 9(12) COMP.
05 LOW-RANGE PIC 9(12) VALUE ZERO.
05 HIGH-RANGE PIC 9(12) VALUE ZERO.
05 YOU-PROVIDE-SEED PIC X VALUE SPACE.
05 RUN-AGAIN PIC X VALUE SPACE.
05 PAUSE-FOR-A-SECOND PIC X VALUE SPACE.
01 SEED-TIME.
05 HOURS PIC 99.
05 MINUTES PIC 99.
05 SECONDS PIC 99.
05 MS PIC 99.
*
* LINKAGE SECTION.
* Not used during testing
01 RANDGEN-AREA.
05 COUNT-OF-ACCESSES PIC 9(12) VALUE ZERO.
05 RANDOM-NUMBERS-NEEDED PIC 9(12) VALUE ZERO.
05 RANDOM-NUMBER PIC 9(12) VALUE ZERO.
05 RANDOM-MSG PIC X(60) VALUE SPACE.
*
* PROCEDURE DIVISION USING RANDGEN-AREA.
* Not used during testing
*
PROCEDURE DIVISION.
100-RANDGEN-EDIT-HOUSEKEEPING.
MOVE SPACE TO RANDOM-MSG.
IF RANDOM-NUMBERS-NEEDED = ZERO
DISPLAY 'RANDOM-NUMBERS-NEEDED ' NO ADVANCING
ACCEPT RANDOM-NUMBERS-NEEDED.
IF RANDOM-NUMBERS-NEEDED NOT NUMERIC
MOVE 'RANDOM-NUMBERS-NEEDED NOT NUMERIC' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF RANDOM-NUMBERS-NEEDED = ZERO
MOVE 'RANDOM-NUMBERS-NEEDED = ZERO' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF COUNT-OF-ACCESSES NOT NUMERIC
MOVE 'COUNT-OF-ACCESSES NOT NUMERIC' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF COUNT-OF-ACCESSES GREATER THAN RANDOM-NUMBERS-NEEDED
MOVE 'COUNT-OF-ACCESSES > THAT RANDOM-NUMBERS-NEEDED' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
IF YOU-PROVIDE-SEED = SPACE AND RANDOM-NUMBER = ZERO
DISPLAY 'DO YOU WANT TO PROVIDE SEED Y OR N: '
NO ADVANCING
ACCEPT YOU-PROVIDE-SEED.
IF RANDOM-NUMBER = ZERO AND
(YOU-PROVIDE-SEED = 'Y' OR 'y')
DISPLAY 'ENTER SEED ' NO ADVANCING
ACCEPT RANDOM-NUMBER.
IF RANDOM-NUMBER NOT NUMERIC
MOVE 'RANDOM-NUMBER NOT NUMERIC' TO RANDOM-MSG
GO TO 900-EXIT-RANDGEN.
200-RANDGEN-DATA-HOUSEKEEPING.
MOVE FUNCTION CURRENT-DATE (9:8) TO SEED-TIME.
IF COUNT-OF-ACCESSES = ZERO
COMPUTE LOW-RANGE =
((SECONDS + HOURS + MINUTES + MS) * 1753).
COMPUTE RANDOM-NUMBER-BUILT = RANDOM-NUMBER + LOW-RANGE.
COMPUTE HIGH-RANGE = RANDOM-NUMBERS-NEEDED + LOW-RANGE.
MOVE X2-POWER TO 2X2.
300-SET-2X2-DIVISOR.
IF 2X2 < (HIGH-RANGE + 1)
COMPUTE 2X2 = 2X2 * X2-POWER
GO TO 300-SET-2X2-DIVISOR.
* *********************************************************
* IF FIRST TIME THROUGH AND YOU WANT TO BUILD A SEED. *
* *********************************************************
IF COUNT-OF-ACCESSES = ZERO AND RANDOM-NUMBER = ZERO
COMPUTE RANDOM-NUMBER-BUILT =
((SECONDS + HOURS + MINUTES + MS) + HIGH-RANGE).
IF COUNT-OF-ACCESSES = ZERO
DISPLAY 'SEED TIME ' SEED-TIME
' RANDOM-NUMBER-BUILT ' RANDOM-NUMBER-BUILT
' LOW-RANGE ' LOW-RANGE.
* *********************************************
* END OF BUILDING A SEED IF YOU WANTED TO *
* *********************************************
* ***************************************************
* THIS PROCESS IS WHERE THE RANDOM-NUMBER IS BUILT *
* ***************************************************
400-RANDGEN-FORMULA.
COMPUTE FIRST-PART = (5 * RANDOM-NUMBER-BUILT) + 7.
DIVIDE FIRST-PART BY 2X2 GIVING WORKING-NUMBER
REMAINDER RANDOM-NUMBER-BUILT.
IF RANDOM-NUMBER-BUILT > LOW-RANGE AND
RANDOM-NUMBER-BUILT < (HIGH-RANGE + 1)
GO TO 600-RANDGEN-CLEANUP.
GO TO 400-RANDGEN-FORMULA.
* *********************************************
* GOOD RANDOM NUMBER HAS BEEN BUILT *
* *********************************************
600-RANDGEN-CLEANUP.
ADD 1 TO COUNT-OF-ACCESSES.
COMPUTE RANDOM-NUMBER =
RANDOM-NUMBER-BUILT - LOW-RANGE.
* *******************************************************
* THE NEXT 3 LINE OF CODE ARE FOR TESTING ON CONSOLE *
* *******************************************************
DISPLAY RANDOM-NUMBER.
IF COUNT-OF-ACCESSES < RANDOM-NUMBERS-NEEDED
GO TO 100-RANDGEN-EDIT-HOUSEKEEPING.
900-EXIT-RANDGEN.
IF RANDOM-MSG NOT = SPACE
DISPLAY 'RANDOM-MSG: ' RANDOM-MSG.
MOVE ZERO TO COUNT-OF-ACCESSES RANDOM-NUMBERS-NEEDED RANDOM-NUMBER.
MOVE SPACE TO YOU-PROVIDE-SEED RUN-AGAIN.
DISPLAY 'RUN AGAIN Y OR N '
NO ADVANCING.
ACCEPT RUN-AGAIN.
IF (RUN-AGAIN = 'Y' OR 'y')
GO TO 100-RANDGEN-EDIT-HOUSEKEEPING.
ACCEPT PAUSE-FOR-A-SECOND.
GOBACK.

shantheri shenoy

unread,
Jan 18, 2023, 4:39:31 AM1/18/23
to
sir could pls explain the program

Bill Gunshannon

unread,
Jan 18, 2023, 8:42:56 AM1/18/23
to
On 1/18/23 04:39, shantheri shenoy wrote:
> * SUBROUTINE TO GENERATE RANDOM NUMBERS THAT ARE GREATER THAN
> * ZERO AND LESS OR EQUAL TO THE RANDOM NUMBERS NEEDED WITH NO
> * DUPLICATIONS. (CALL "RANDGEN" USING RANDGEN-AREA.)

With this as a requirement the numbers can not be random. True
random numbers can duplicate. In fact, a duplicate is just as
random as a non-duplicate and just as possible.

bill

0 new messages