Page 1 of 2

Change Random number

PostPosted: Wed May 21, 2014 9:17 am
by thia_88
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

Re: Random

PostPosted: Wed May 21, 2014 10:45 am
by Aki88
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!

Re: Random

PostPosted: Wed May 21, 2014 11:12 am
by BillyBoyo
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.

Re: Change Random number

PostPosted: Wed May 21, 2014 4:01 pm
by steve-myers
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.

Re: Change Random number

PostPosted: Wed May 21, 2014 4:27 pm
by enrico-sorichetti
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

Re: Change Random number

PostPosted: Wed May 21, 2014 7:07 pm
by Ed Goodman
Brute force it!
Perform until WS-RAND > 99 and WS-RAND < 1999
COMPUTE WS-RAND = FUNCTION RANDOM (1132237456754)
end-perform

Re: Change Random number

PostPosted: Wed May 21, 2014 7:41 pm
by BillyBoyo
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.

Re: Change Random number

PostPosted: Thu May 22, 2014 1:38 am
by Ed Goodman
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.

Re: Change Random number

PostPosted: Thu May 22, 2014 6:24 am
by thia_88
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

Re: Change Random number

PostPosted: Thu May 22, 2014 8:28 am
by Robert Sample
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).