SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow


ABAP Tutorial - How to Generate Random Number for a Given Range of Numbers using RANDOM_I2 Function Module


ABAP developers can generate random numbers for a given range of numbers using different methods.
In your SAP system, there may be more methods for ABAP random number generation.
You can run the SAP SE37 transaction code and search for "RANDOM*" for ready to use function modules in your ABAP codes in your current SAP system.
One function module used to generate random number is QF05_RANDOM_INTEGER.

Now in this ABAP tutorial, I want to give an ABAP example for generating random numbers using function module RANDOM_I2.
Below example ABAP codes and the sample call for ABAP function module can be seen in order to create random numbers.





The local variables lv_RangeMaximum and lv_RangeMinimum are in DATATYPE-INTEGER2 data type and the input parameters of function module RANDOM_I2.
These two parameters define the range of numbers by identifying the start and the end of the numbers range.
lv_RndI is used to get the output generated random number from the QF05_RANDOM_INTEGER function module.

REPORT ZGENERATERANDOMNUMBER.

DATA :
  gv_i TYPE I,
  gv_Min TYPE DATATYPE-INTEGER2,
  gv_Max TYPE DATATYPE-INTEGER2,
  gv_Rnd TYPE DATATYPE-INTEGER2.

START-OF-SELECTION.

gv_Min = 1.
gv_Max = 100.
gv_i = 10.

WHILE gv_i > 0.
  PERFORM u_perform_randomread USING gv_Min gv_Max gv_Rnd.
  WRITE :/ 'The Random Number Generated by RANDOM_I2 is : ', gv_Rnd.
  gv_i = gv_i - 1.
ENDWHILE.

END-OF-SELECTION.

FORM U_PERFORM_RANDOMREAD USING
    p_RangeMinimum TYPE DATATYPE-INTEGER2
    p_RangeMaximum TYPE DATATYPE-INTEGER2
    p_RandomTicketNumber TYPE DATATYPE-INTEGER2.

CALL FUNCTION 'RANDOM_I2'
EXPORTING
  RND_MIN = p_RangeMinimum
  RND_MAX = p_RangeMaximum
IMPORTING
  RND_VALUE = p_RandomTicketNumber.

ENDFORM.
Code

And here is the output of the example ABAP code executed for 10 random random number generation.
The above example ABAP code can be used by ABAP developers in order to generate random number in ABAP.

random-number-generation-in-abap-using-random_i2



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.