## Function Random

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

### Function Random

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
thia_88

Posts: 12
Joined: Wed May 07, 2014 8:48 am
Has thanked: 0 time
Been thanked: 0 time

### Re: Function Random

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
thia_88

Posts: 12
Joined: Wed May 07, 2014 8:48 am
Has thanked: 0 time
Been thanked: 0 time

### Re: Function Random

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.
Robert Sample
Global moderator

Posts: 3671
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 273 times

### Re: Function Random

Hi Mr.Robert Sample,

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

Regards,
Thia
thia_88

Posts: 12
Joined: Wed May 07, 2014 8:48 am
Has thanked: 0 time
Been thanked: 0 time

### Re: Function Random

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.
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

### Re: Function Random

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator

Posts: 2968
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 160 times

### Re: Function Random

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?
thia_88

Posts: 12
Joined: Wed May 07, 2014 8:48 am
Has thanked: 0 time
Been thanked: 0 time

### Re: Function Random

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.
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

### Re: Function Random

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>
Aki88

Posts: 381
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 36 times

### Re: Function Random

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.
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Next