IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Random number generation with PL/I program.


IBM Mainframe Forums -> PL/I & Assembler
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
satish.ms10

Active User


Joined: 10 Aug 2009
Posts: 184
Location: India

PostPosted: Wed Jul 21, 2010 2:48 pm
Reply with quote

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
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Wed Jul 21, 2010 2:58 pm
Reply with quote

start from
www-03.ibm.com/systems/z/os/zos/bkserv/index.html

proceed to Your zOS bookshelf ( for example )
www-03.ibm.com/systems/z/os/zos/bkserv/zshelves9.html

anf the to the Language environment bookshelf
publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/Shelves/CEE2BK80

search for random or CEERAN0 ( I knew what to search for ) and ...
publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/ceea3180/2.2.5.61?ACTION=MATCHES&REQUEST=ceeran0&TYPE=FUZZY&SHELF=CEE2BK80.bks&DT=20070428023816&CASE=&searchTopic=TOPIC&searchText=TEXT&searchIndex=INDEX&rank=RANK&ScrollTOP=FIRSTHIT#FIRSTHIT

better to use a standard method rather than using Your own
Back to top
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Wed Jul 21, 2010 8:33 pm
Reply with quote

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
View user's profile Send private message
satish.ms10

Active User


Joined: 10 Aug 2009
Posts: 184
Location: India

PostPosted: Thu Jul 22, 2010 12:29 pm
Reply with quote

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
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Thu Jul 22, 2010 12:36 pm
Reply with quote

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
View user's profile Send private message
satish.ms10

Active User


Joined: 10 Aug 2009
Posts: 184
Location: India

PostPosted: Thu Jul 22, 2010 1:10 pm
Reply with quote

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
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Thu Jul 22, 2010 1:18 pm
Reply with quote

"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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Jul 22, 2010 4:54 pm
Reply with quote

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
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Thu Jul 22, 2010 5:02 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> PL/I & Assembler

 


Similar Topics
Topic Forum Replies
No new posts Using API Gateway from CICS program CICS 0
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Substring number between 2 characters... DFSORT/ICETOOL 2
No new posts Generate random number from range of ... COBOL Programming 3
No new posts Increase the number of columns in the... IBM Tools 3
Search our Forums:

Back to Top