Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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?

replied

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.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question