Using CEERANO in RPGLE

0 pts.
Tags:
Development
Can anyone provide me with a small RPGLE program that uses CEERANO to generate a random number?
ASKED: June 24, 2005  3:46 PM
UPDATED: June 26, 2005  12:14 AM

Answer Wiki

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.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following