Skip to Content

Random number using CL_ABAP_RANDOM_INT

Hi experts,

I am trying to get a random number using the class CL_ABAP_RANDOM_INT, but the following code always return 22.

DATA:
lo_ran TYPE REF TO CL_ABAP_RANDOM_INT,
lv_i TYPE i.

lo_ran = CL_ABAP_RANDOM_INT=>CREATE( MIN = 5 MAX = 25 ).
lv_i = lo_ran->GET_NEXT( ).

WRITE lv_i.

Do you know, how to get random numbers using this class?

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    Jul 30, 2012 at 08:44 AM

    You will need a seed value at the initialization, to start with different start values. The instance will then produce a series of numbers which are approximately uniformly distributed.

    report  zz_random_int.
    data:
    lo_ran type ref to cl_abap_random_int,
    lv_i type i,
    lv_seed type i.lv_seed = sy-timlo.
    lo_ran = cl_abap_random_int=>create( min = 5 max = 25 seed = lv_seed ).
    do 20 times.
    lv_i = lo_ran->get_next( ).
    write / lv_i.
    enddo.
    Add comment
    10|10000 characters needed characters exceeded

  • Jul 30, 2012 at 07:36 AM

    Hiii ,

    Use this FM - SXMS_CREATE_RANDOM_NUMBER. 😊

    Add comment
    10|10000 characters needed characters exceeded