View previous topic :: View next topic
|
Author |
Message |
satish.ms10
Active User
Joined: 10 Aug 2009 Posts: 184 Location: India
|
|
|
|
Hi,
I have written a small program to generate random number in PL/I.
Here is the code:
Code: |
TEST888: PROC OPTIONS(MAIN);
DCL
RANDVAL FLOAT DEC(2);
DECLARE RANDOM GENERIC (
RANDOM0 WHEN ( ),
RANDOM1 WHEN (*) );
DCL SYSPRINT STREAM FILE;
DO I=1 TO 5;
RANDVAL=RANDOM();
PUT SKIP LIST ('RANDOM NUMER:',RANDVAL);
END;
PUT SKIP LIST ('END...');
RANDOM0:
PROCEDURE RETURNS (FLOAT BINARY (53) );
DECLARE R_SEED EXTERNAL STATIC FIXED BINARY (31)
INITIAL (1);
RETURN (RANDOM(R_SEED));
END RANDOM0;
(NOSIZE, NOFIXEDOVERFLOW):
RANDOM1:
PROCEDURE (SEED) RETURNS (FLOAT BINARY (53));
DECLARE SEED FIXED BINARY (31);
DECLARE R_SEED EXTERNAL STATIC FIXED BINARY (31)
INITIAL (1);
DECLARE (UNSPEC, ABS) BUILTIN;
DECLARE CHOPPER FIXED BINARY (31) STATIC
INITIAL (2147483647);
IF SEED <= 0 THEN SIGNAL ERROR;
IF SEED >= 2147483646 THEN SIGNAL ERROR;
SEED = SEED * 5 - 3;
IF SEED < 0 THEN
SEED = (SEED + CHOPPER) + 1;
IF SEED < 0 THEN
UNSPEC (SEED) = UNSPEC (SEED) & UNSPEC (CHOPPER);
IF SEED < 0 THEN
SEED = ABS(SEED);
R_SEED = SEED;
RETURN (SEED/2147483648E0);
END RANDOM1;
END TEST888;
|
Output of the above program is like below:
Code: |
********************************* TOP OF DATA **********************************
RANDOM NUMER: 9.3E-10
RANDOM NUMER: 3.2E-09
RANDOM NUMER: 1.4E-08
RANDOM NUMER: 7.3E-08
RANDOM NUMER: 3.6E-07
END...
******************************** BOTTOM OF DATA ********************************
|
But I the expected results are
Code: |
********************************* TOP OF DATA **********************************
RANDOM NUMER: 9
RANDOM NUMER: 3
RANDOM NUMER: 1
RANDOM NUMER: 7
RANDOM NUMER: 3
END...
******************************** BOTTOM OF DATA ********************************
|
Could you please let me know what change I need to do to get desired results?
Kindly let me know if we have any other way to generate random number in PL/I program.
Awaiting your kind reply.
Thanks in advance. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
satish.ms10 wrote: |
Hi,
I have written a small program to generate random number in PL/I. |
Wow, and it doesn't even work...
Next time, RTFM, PL/I, since at least Enterprise PL/I V3R5, has a builtin with the rather obvious name "RANDOM", which generates a FLOAT BIN (53) pseudo-random number between 0 and 1. |
|
Back to top |
|
|
satish.ms10
Active User
Joined: 10 Aug 2009 Posts: 184 Location: India
|
|
|
|
Hi Prino,
I have gone thru the manual and used RANDOM function but it is giving errors at the time of compilation
My code is:
Code: |
DCL
RAND2 ENTRY,
RAND3 FLOAT DEC(10),
RANDVAL FLOAT DEC(2);
DECLARE RANDOM GENERIC (
RAND2 WHEN (FIXED),
RAND3 WHEN (FLOAT) );
DO I=1 TO 5;
RANDVAL=RANDOM(1);
PUT SKIP LIST ('RANDOM NUMER:',RANDVAL);
END; |
The error messages which I get at the time of compilation are:
Code: |
COMPILER DIAGNOSTIC MESSAGES
ERROR ID L STMT MESSAGE DESCRIPTION
SEVERE AND ERROR DIAGNOSTIC MESSAGES
IEL0445I S 4 'RAND3' IN 'GENERIC' SPECIFICATION IS NOT AN ENTRY NAME AND IS IGNORED.
|
Could you please let me know how to declare and use this function?
Thanks in advance. |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
satish.ms10 wrote: |
I have gone thru the manual and used RANDOM function but it is giving errors at the time of compilation
My code is:
Code: |
DCL
RAND2 ENTRY,
RAND3 FLOAT DEC(10),
RANDVAL FLOAT DEC(2);
DECLARE RANDOM GENERIC (
RAND2 WHEN (FIXED),
RAND3 WHEN (FLOAT) );
DO I=1 TO 5;
RANDVAL=RANDOM(1);
PUT SKIP LIST ('RANDOM NUMER:',RANDVAL);
END; |
The error messages which I get at the time of compilation are:
Code: |
COMPILER DIAGNOSTIC MESSAGES
ERROR ID L STMT MESSAGE DESCRIPTION
SEVERE AND ERROR DIAGNOSTIC MESSAGES
IEL0445I S 4 'RAND3' IN 'GENERIC' SPECIFICATION IS NOT AN ENTRY NAME AND IS IGNORED.
|
Could you please let me know how to declare and use this function.
|
Is
Code: |
dcl random builtin; |
too hard to understand for you gray cell? |
|
Back to top |
|
|
satish.ms10
Active User
Joined: 10 Aug 2009 Posts: 184 Location: India
|
|
|
|
Hi Prino,
We are having "IBM PL/I for MVS & VM Ver 1 Rel 1" at our shop.
So I got code sample (which I mentioned in the beginning of this post) to generate random number from google. That code is giving following results
Code: |
RANDOM NUMER: 9.3E-10
RANDOM NUMER: 3.2E-09
RANDOM NUMER: 1.4E-08
RANDOM NUMER: 7.3E-08
RANDOM NUMER: 3.6E-07
END...
|
Which I want output as a whole number. Could you please suggest me where I need to do changes to my code? and what change it is?
Kindly advise me. |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
"The number is too small!"
Ever thought of multiplying it by a suitable factor? Holy sith, are you really this thick, or just pretending? |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Either you're a student or you slept through your programming classes (or they've changed the classes drastically from when I was in school). My classes taught me the output of a random number generator was a value uniformly distributed between zero and one. So I wanted a throw of a die, I'd multiply the random number by six, add one, and take the integer value. And this was taught in multiple programming classes. |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
I don't understand why the OP's orig numbers all have large negative exponents - even those are not uniform between 0-1. Was that not the intended interval?
This question, seems to me, needs an answer before converting to an integer range. (Or, of course, the correct answer above - use the BUILTIN function.)
I must have missed something here. |
|
Back to top |
|
|
|