Random Number Generation in RPGLE

pts.
Tags:
Development
I need to generate a random number between 1 and 100 in RPGLE. Does anyone have an algorithm to do it?

Answer Wiki

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

The following code works for me. Add it to an SQL RPGLE program. What the SQL RAND() does is generate a random decimal value less than 1. Multiplying it times 100 into an integer variable results in a value from 1 to 100 after rounding. The dummy FROM clause is entered as written below.

This can work with any range of numbers. Say you want a value 1 to 250. Just change the * 100 to * 250.

D Random S 5I 0 Inz

C/Exec SQL
C+ Select Rand() * 100 Into :Random
C+ From SYSIBM/SYSDUMMY1
C/End-Exec

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