Change Random number



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

Change Random number

Postby thia_88 » Wed May 21, 2014 9:17 am

Hi,

Does anyone know how to change random number(1132237456754) to any number in range between 100 and 2000?

Does anyone know the formula...?
p/s : this is my assigment question

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

Re: Random

Postby Aki88 » Wed May 21, 2014 10:45 am

Hello,

IMHO, there is no formula (that I am aware of; maybe some arithematic progression somewhere ;) ) as such to achieve what you are requesting for.
Rather what is advisable is to build a business logic around the requirement given to you; which in turn becomes the 'formula', or more aptly put - the algorithm.

How about doing something as:

COMPUTE A = FUNCTION RANDOM (1132237456754)
DISPLAY 'A:' A.
IF (A < 100)
    COMPUTE B = A + 999
    COMPUTE C = FUNCTION RANDOM(B)
    DISPLAY 'C:' C
ELSE
    IF (A > 1999)
       COMPUTE D = A - 999
       COMPUETE E = FUNCTION RANDOM(D)
       DISPLAY 'E:' E
    END-IF
END-IF.


The above piece is not a solution for your requirement, it is just a sample code to show you what you can do with an algorithm; as a beginner, you can resort to hit-trial for a range of values which you can use; it is not necessary that you use FUNCTION RANDOM, what we usually do is build our own logic to generate a PSEUDO-RANDOM number, seldom using system time as input and playing around it.

So, once again, think of an algorithm, play around it; that will help you explore more functions as well, and improve your programming/analytical skills.

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

Re: Random

Postby BillyBoyo » Wed May 21, 2014 11:12 am

If you were to already have code which gives you 0-1900 it should be easy to map that to 100-2000. 100 - 0 = 100, 2000 - 1900 = 100, 1000 - 900 = 100.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Change Random number

Postby steve-myers » Wed May 21, 2014 4:01 pm

thia_88 wrote:Hi,

Does anyone know how to change random number(1132237456754) to any number in range between 100 and 2000?

Does anyone know the formula...?
p/s : this is my assigment question

Thanks,
rati
This is both simpler and more complicated than you imagine.
  • What statistical distribution do you want? Usually you want the normal distribution, but others are possible. In any event, this is usually dictated by the random number generator.
  • All random number generators produce a number in a range of numbers, typically 0 to some maximum value for an integer random number generator, or 0 to 1 for a floating point generator. What is the range for for the generator that produced your 1132237456754? If you do not know this no one can help you.
  • Once you know the range, solve this formula 1132237456754/max = x/(2000-100). The number you want will be x+100.
steve-myers
Global moderator
 
Posts: 2105
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 243 times

Re: Change Random number

Postby enrico-sorichetti » Wed May 21, 2014 4:27 pm

just to add some more
the same concept could be applied using MIN instead of MAX ...

but ...
my preference goes to the REM function

new = FUNCTION REM(Your-random,1900) + 100

no need to know min and max

PS.
for a range of 1900 integers is not worth ( IMO ) to worry about the statistical properties
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: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: Change Random number

Postby Ed Goodman » Wed May 21, 2014 7:07 pm

Brute force it!
Perform until WS-RAND > 99 and WS-RAND < 1999
COMPUTE WS-RAND = FUNCTION RANDOM (1132237456754)
end-perform
Ed Goodman
 
Posts: 341
Joined: Thu Feb 24, 2011 12:05 am
Has thanked: 3 times
Been thanked: 17 times

Re: Change Random number

Postby BillyBoyo » Wed May 21, 2014 7:41 pm

Did you try that Ed? I'd suggest you code a very small TIME= on the step. With a seed, you always get the same value returned. The value is between 0 and 1.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Change Random number

Postby Ed Goodman » Thu May 22, 2014 1:38 am

Yeah, I know...sometimes you have to be absurd to cut through the fog. Once the student sees just how bad an answer it is, they MAY stop and think about doing it better.
Ed Goodman
 
Posts: 341
Joined: Thu Feb 24, 2011 12:05 am
Has thanked: 3 times
Been thanked: 17 times

Re: Change Random number

Postby thia_88 » Thu May 22, 2014 6:24 am

Ed Goodman,

You can always message and ask me if you are unclear with my question...thats how it should be...pardon me if i wrong.

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

Re: Change Random number

Postby Robert Sample » Thu May 22, 2014 8:28 am

thia_88, your FIRST problem is terminology. You use the term "random number" which in COBOL is an output of the FUNCTION RANDOM intrinsic and represents a floating-point value between 0 and 1.

Your second problem is you do not appear to have much of a grasp of computational mathematics (usually covered in a computer science class in college).

You appear to want to take an arbitrary value and convert it to a range. If you want to do a proportional conversion, as stated elsewhere you MUST know the maximum value that could be returned. If you don't care about proportionality and just want the value converted, this code (untested) will do it:
COMPUTE WS-POWER-OF-10 = FUNCTION INTEGER (FUNCTION LOG10 (WS-FIXED-VALUE ) )
COMPUTE WS-FLOAT-VALUE = WS-FIXED-VALUE / (10 ** WS-POWER-OF-10)
COMPUTE WS-RANGE-VALUE = WS-FLOAT-VALUE * (2000 - 100) + 100
where WS-FIXED-VALUE is your arbitrary value, WS-FLOAT-VALUE is a COMP-2 variable, WS-RANGE-VALUE is the ranged value, and WS-POWER-OF-10 is a 3-digit integer (COBOL floating point values cannot have more than 3 digits before the decimal in a LOG10 result).
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post