Wednesday, May 28, 2008

SAP ABAP Random Number Generator

SAP ABAP Random Number Generator

If you wish to generate a Random Number in ABAP you can use the following function module.

QF05_RANDOM_INTEGER

Here you need to input 2 numbers ie the upper limit and the lower limit. This works only for integers.

Please find the code below.

REPORT ZEX_RANDOMNUMBER .

Parameters: p_larg like QF00-RAN_INT,
            p_small like QF00-RAN_INT.

Data: d_result like QF00-RAN_INT.

CALL FUNCTION 'QF05_RANDOM_INTEGER'
 EXPORTING
   RAN_INT_MAX         = p_larg
   RAN_INT_MIN         = p_small
 IMPORTING
   RAN_INT             = d_result
 EXCEPTIONS
  INVALID_INPUT       = 1
   OTHERS              = 2
          .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


Write:/ 'The Number is = ' , d_result.



If you enter  100 and 5 then  the result would be any number between 5 and 100 inclusive of 5 and 100.

 

No comments:

Post a Comment