Using CEERANO in RPGLE

pts.
Tags:
Development
Can anyone provide me with a small RPGLE program that uses CEERANO to generate a random number?

Thanks. We'll let you know when a new response is added.

This is taken from a response to the same question on the www.code400.com RPG programming forum.

…… That same algorithm can be used to calculate a number between two
arbitrary numbers. Just use variables for the upper and lower ends
of the range. The following code demonstrates this by receiving
parameters called LOWNO and HIGHNO. It returns the result in a
third parameter called RESULT:

H DFTACTGRP(*NO) ACTGRP(‘QILE’)

D RANDOM PR extpgm(‘RANDOM’)
D lowno 5P 0 const
D highno 5P 0 const
D result 5P 0

D RANDOM PI
D lowno 5P 0 const
D highno 5P 0 const
D result 5P 0

D CEERAN0 PR
D seed 10I 0
D ranno 8F
D fc 12A options(*omit)

D seed s 10I 0 inz(0)
D rand s 8F
D range s 5P 0

/free
range = (highno – lowno) + 1;
CEERAN0( seed : rand : *omit );
result = %int(rand * range) + lowno;
return;
/end-free
Random numbers on computers are generated by an equation.
When the equation has the same input value, it’ll always give
the same results. To make them seem random, we use a seed value.
Therefore it’s important that the seed value is different every
time you call this API.

For CEERAN0, if you pass a seed of 0, the API will generate a
seed from the current time. That way, it will be different each
time the program is called. CEERAN0 will output a new seed that
can be used to help randomize subsequent calls.

If you look closely at the examples above, you’ll see that they
don’t set on *INLR. The reason for this is that the program will
remain in memory and it will retain the seed value. That way, if
you call it again, it’ll use the seed value that was retained
from the previous call.