Generate a random number in ABAP

Generate a random number in ABAP

Use class CL_ABAP_RANDOM_INT to generate a random integer number :


DATA(lv_random_num) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                                                  min  = 1
                                                  max  = 100 )->get_next( ).
* lv_random_num will be type i

If several numbers have to be generated :


  DATA(lo_random) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
                                                min  = 1
                                                max = 100 ).

  DO 5 TIMES.
    DATA(lv_random_num) = lo_random->get_next( ).
    " Write lv_random_num.

  ENDDO.

For other output types, other classes are available :

  • CL_ABAP_RANDOM_INT for type i
  • CL_ABAP_RANDOM_INT8 for type int8
  • CL_ABAP_RANDOM_FLOAT for type f
  • CL_ABAP_RANDOM_PACKED for type p
  • CL_ABAP_RANDOM_PACKED_DEC1 to CL_ABAP_RANDOM_PACKED_DEC14 for type p with 1 to 14 decimal places.
  • CL_ABAP_RANDOM_DECFLOAT16 for type decfloat16
  • CL_ABAP_RANDOM_DECFLOAT34 for type decfloat34

To generate random currency number, you can use FM RANDOM_AMOUNT :


  CALL FUNCTION 'RANDOM_AMOUNT'
    EXPORTING
      rnd_min    = '1'
      rnd_max    = '100'
      valcurr    = 'EUR'
    IMPORTING
      rnd_amount = lv_rand_amount
    EXCEPTIONS
      OTHERS     = 1.

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply