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
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
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
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.)
>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/
|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
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...
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.
<<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
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! :-(
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