Our program will demonstrate generating random numbers. Here’s the full source code:
This COBOL program demonstrates the generation of random numbers:
GENERATE-RANDOM-INTEGERS generates two random integers between 0 and 99.
GENERATE-RANDOM-FLOATS generates a random float between 0 and 1, then generates two random floats between 5 and 10.
GENERATE-SEEDED-RANDOMS uses the current seconds as a seed to generate two random integers between 0 and 99.
To run the program, compile the COBOL code and execute the resulting program:
Note that the generated numbers may be different when you run the sample.
In COBOL, we use the FUNCTION RANDOM to generate random numbers. Without arguments, it returns a float between 0 and 1. With an integer argument, it uses that as a seed.
COBOL doesn’t have built-in support for different random number generators like PCG. For more complex random number generation, you might need to implement custom algorithms or use external libraries.