Page 1 of 2

Function Random

PostPosted: Fri May 09, 2014 8:07 am
by thia_88
Hi,

01 W-2000 PIC 9(4) VALUE IS 2000.
01 W-100 PIC 9(3) VALUE IS 100.

COMPUTE RANDOMS-NUM = FUNCTION RANDOM * (W-2000/5 - W-100/5)
COMPUTE RANDOMS-INCOME = RANDOMS-NUM*5

Can anyone explain what this the purpose we divide and multiply with 5.
What is the purpose?

Thanks,
thia

Re: Function Random

PostPosted: Fri May 09, 2014 8:10 am
by thia_88
Im not sure whether my understanding is correct or not.
Please correct me if i wrong...

1. Function random will only return value in between 0 and 1.
2. We have to multiply with (W-2000/5 - W-100/5) because our rules to get random numbers in between 100 and 2000.
3. We compute this ( RANDOMS-NUM * 5) so that all the random value is dividable by 5.

Thanks,
thia

Re: Function Random

PostPosted: Fri May 09, 2014 9:33 am
by Robert Sample
The provided code will generate pseudorandom numbers between 0 and 1900, but only numbers divisible by 5. The sequence will contain values less than 100. This may be a coding mistake, or it may be done for other reasons not known. Note that this is NOT a random sequence since the starting seed is known to be constant.

To convert a random value between zero and one (such as what FUNCTION RANDOM returns) into an integer value between A and B (where A and B are non-negative integers and A < B), multiply the generated value by (B-A) and add A to the result. Convert the value to an integer either after the multiplication or after the addition.

Re: Function Random

PostPosted: Fri May 09, 2014 10:04 am
by thia_88
Hi Mr.Robert Sample,

Maybe the purpose is to get random numbers in between 100 and 2000 and also divisible by 5.
Thank you for the reply.

Regards,
Thia

Re: Function Random

PostPosted: Fri May 09, 2014 1:56 pm
by BillyBoyo
The range will be as Robert has stated. To get 100-2000, 100 would need to be added (because when zero is returned by random, the result of the existing COMPUTE will be zero).

Why it is coded in that way is unclear, as it could just multiply by 1900 or by 1900/5. If W-2000 and W-100 were called something meaningful, the answer could be self-evident.

If "divisible by 5" is the requirement (probably is) there are other ways, but that should achieve it.

Re: Function Random

PostPosted: Fri May 09, 2014 2:55 pm
by enrico-sorichetti
You should really ask the person who did the coding or the design

01 W-2000 PIC 9(4) VALUE IS 2000.
01 W-100 PIC 9(3) VALUE IS 100.

COMPUTE RANDOMS-NUM = FUNCTION RANDOM * (W-2000/5 - W-100/5)
COMPUTE RANDOMS-INCOME = RANDOMS-NUM*5


COMPUTE RANDOMS-INCOME = FUNCTION RANDOM * 1900

could achieve the same result


to help people to give You a better explanation of what is going on
You should have also posted the declarations of
RANDOMS-NUM and RANDOMS-INCOME
the ODD CODING could be explained by looking at them

Re: Function Random

PostPosted: Wed May 14, 2014 12:20 pm
by thia_88
My Assignment Question : Assign a Random Value to each cells(array table), the value must be able to be divided by 5 (multiple of 5) and in between 100 to 2000.

So this is the reason why i set max value to 2000 and min value to 100.

Is there any better way to assign random value to cells?
Now my question is whether my formula is accurate to display the range?

Re: Function Random

PostPosted: Wed May 14, 2014 12:58 pm
by BillyBoyo
2000 - 100 = 1900. 1900 / 5 = 380. 0 * 380 = 0. 0 * 5 = 0.

2000 - 100 = 1900. 1900 / 5 = 380. 1 * 380 = 380. 380 * 5 = 1900.

So, as Robert stated days ago, this produces values between 0 and 1900, not 100 and 2000.

What is a cell?

To tell if it is accurate, why don't you run it? Or work it through with pencil and paper?


Your calculation, even though we now know you wrote it yourself, will not produce your desired results.

Re: Function Random

PostPosted: Wed May 14, 2014 1:25 pm
by Aki88
Hello,

thia_88 wrote:My Assignment Question : Assign a Random Value to each cells(array table), the value must be able to be divided by 5 (multiple of 5) and in between 100 to 2000.


Aside from what Billy has already said; I am being an optimist here :)

What is the required length of this array table/cell; meaning, how many random numbers/multiples of 5 are to be generated in one single run of the program or are these tables to be used for something else? (because we are talking cells/array tables)

A beginner level method to achieve the requisite <if I have understood the requirement, even a bit; minus the cell part>, can be by writing an algorithm similar to that of a multiplication table.
Difference here being, the formula will look something as: X * 5; where X will be a randomly generated number between your range of values.
You can accept the value of a number via user input, have an IF condition to check whether this input number is in the requisite range you need; IF yes, then proceed to randomize it using this as a SEED VALUE (the arguement value) for RANDOM function; assign this value to X; have a conditional check on this randomly generated value, as to whether this is within your requisite range, IF yes, proceed on to multiply/divide/do the remaining requisite operations with this randomly generated number.

If it is an array of values you need; then it gets a little more complicated as you might've to add a PERFORM VARYING loop, which would take the newly generated RANDOM value as a new SEED VALUE and proceed with RANDOMizing it.

<All this, if I understood the requirement correctly; and yes, it is a bad design with all those IFs, but it is an absolute beginner logic; have a look at multiplication table generation logic, the algorithm for this requirement might be very similar to that>

Re: Function Random

PostPosted: Wed May 14, 2014 2:28 pm
by BillyBoyo
You should only specify a seed to RANDOM once per program. The SEED is a starting-point for the pseudo-random number generator. Te same seed will always give the same value. Think of it as a circle. The seed tells you which point on the circumference to start. Once you have had the seed, then the next number is just given to you. The sequence is always entirely repeatable, if the seed is known. If you repeatedly use a seed, you will just be going to fixed pre-set points on the circle.

OK, in a table with OCCURS, we tend to call them Elements. This, if you like, is Mainframe Jargon. If you use other Jargon, confusions can arise, particularly when the same Jargon-word means different things. If your computing background is other than Mainframe, it can be a good idea to stick to English, or explain the Jargon you use. To me, a Cell is something in a spreadsheet, and has been for about 30 years. Happens to be a Jargon-word from my experience, and means something somewhat different. So if we both talk "cells" the chance of even agreeing what we are talking about is extremely limited.

To get your range, try the effect of adding 100 after the rest of the calculation.

Indeed, look at taking your random value and multiplying it by 380 and then add 100. This will give you 380 distinct values which end in 0 or 5.