sapdev logo background
sapdev logo sapdev logo
Comments

Generate random number using QF05_RANDOM_INTEGER SAP function module within your ABAP code




I found with this function module that the first number it generates is not always random. I.e. if you execute the ABAP report below multiple times in quick succession each time the initial number generated will be the same. After that however the remaining (2nd & 3rd) numbers do seem to become random. Therefore to use it effectively it could just be a case of ignoring the first number each time a program is executed. Having said all this if there is a few minutes between each execution of QF05_RANDOM_INTEGER then the first number will change!!!!

Copy the below ABAP code into a new report to see this functionality in action and confirm for yourself how random the numbers are. Also see my testing results.

Generate random number
*&---------------------------------------------------------------------*
*& Report  ZQF05_RANDOM_INTEGER.
*&
*&---------------------------------------------------------------------*
*&created by  QF05_RANDOM_INTEGER*
*&
*&---------------------------------------------------------------------*
REPORT  ZQF05_RANDOM_INTEGER.

DATA: ld_ranint type QF00-RAN_INT,
      ld_maxint type QF00-RAN_INT,
      ld_minint type QF00-RAN_INT.

***********************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.
ld_minint = 1.
ld_maxint = 1000.

do 3 times.
CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
  RAN_INT_MAX   = ld_maxint
  RAN_INT_MIN   = ld_minint
IMPORTING
  RAN_INT       =  ld_ranint
EXCEPTIONS
  INVALID_INPUT = 1
  OTHERS        = 2.

 WRITE:/ ld_ranint.
enddo.

Testing results

Initial result


1 second after previous run


1 second after previous run



A few minutes later


1 second after previous run


1 second after previous run

So basically looking at the test results it seems to get its initail result from some kind of incremental number range which gets updated every few minutes, then any further numbers are generated randomly from that. Based on these results i would always ignore the first number generated when using this function module.




comments powered by Disqus