Skip to Content
Technical Articles
Author's profile photo Dmitriy Sokoloff

A Comprehensive Guide to Generating Random Numbers in SAP

Random numbers play an indispensable role in various aspects of programming, such as in simulations, cryptography, testing, and more. These numbers can be utilized for a wide range of applications, from creating unique identifiers to facilitating random sampling. However, SAP’s native programming language, ABAP (Advanced Business Application Programming), does not inherently provide a straightforward function to generate random numbers. Fortunately, there are methods to achieve this. This article provides a comprehensive guide on generating random numbers in SAP, focusing on ABAP code.

Random Number Generation using ABAP

In ABAP, there are several ways to generate random numbers. In this guide, we will discuss two methods:

  1. Using the Function Module QF05_RANDOM_INTEGER.
  2. Using the Function Module SAP_RANDOM_GET when QF05_RANDOM_INTEGER is unavailable.

Method 1: Using ‘QF05_RANDOM_INTEGER’

QF05_RANDOM_INTEGER is a built-in function in newer SAP versions that generates a random integer. The function requires two input parameters – the minimum and maximum number in the desired range.

Here is a sample ABAP code snippet to generate a random number between 1 and 100:

DATA: lv_random TYPE i.

CALL FUNCTION 'QF05_RANDOM_INTEGER'
  EXPORTING
    ran_int_min = 1
    ran_int_max = 100
  IMPORTING
    ran_int     = lv_random.

WRITE: / 'Random Number:', lv_random.

Method 2: Using ‘SAP_RANDOM_GET’

If QF05_RANDOM_INTEGER is not available, you can use the SAP_RANDOM_GET function module. This module generates a random floating point number between 0 (inclusive) and 1 (exclusive). To convert this to an integer in a desired range, you’ll need to do a little bit of additional work. Here’s how:

DATA: lv_random TYPE f,
      lv_random_int TYPE i,
      lv_min TYPE i VALUE 1,
      lv_max TYPE i VALUE 100.

CALL FUNCTION 'SAP_RANDOM_GET'
  IMPORTING
    ran_float   = lv_random.

lv_random_int = lv_min + lv_random * ( lv_max - lv_min + 1 ).
lv_random_int = TRUNC( lv_random_int ).

WRITE: / 'Random Number:', lv_random_int.

This code scales and shifts the random floating point number to your desired range and truncates it to an integer.

Conclusion

While ABAP may not provide an intuitive method for random number generation, it certainly offers the flexibility to achieve this goal. Whether you are using the QF05_RANDOM_INTEGER or SAP_RANDOM_GET function modules, you can efficiently generate random numbers for your SAP applications. Remember, these numbers are pseudo-random, which means they should not be used in security-critical situations where true randomness is required.

Embrace the randomness within ABAP to infuse dynamicity into your programs. Happy coding!

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Lars Hvam
      Lars Hvam

      I've also found function module GENERATE_SEC_RANDOM, which looks crypto secure, however I dont think any of this is marked as released on latest Steampunk 😞

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Have you tried using the classes within than FM directly?

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Rather then FM, use the classes:

      DATA(random_generator) = cl_abap_random=>create( seed = CONV #( sy-uzeit ) )
      DATA(random_number) = random_generator->intinrange( low = 1 high = 10 ).

      for example, will give you a random number between 1 and 10.

      I used this recently to shuffle an internal table

      DATA(in_sequence) = itab_to_shuffle.
      CLEAR itab_to_shuffle.
      GET TIME.
      DATA(random_generator) = cl_abap_random=>create( seed = CONV #( sy-uzeit ) ).
      DATA(count) = lines( in_sequence ).
      DO count TIMES.
        DATA(index) = random_generator->intinrange( low  = 1
                                                    high = count - sy-index + 1 ).
        INSERT in_sequence[ index ] INTO TABLE itab_to_shuffle.
        DELETE in_sequence INDEX index.
      ENDDO.

      Lars Hvam Still pseudorandom, I'm afraid. But cl_abap_random is released and documented.

      Author's profile photo Fabian Lupa
      Fabian Lupa

      If you just want to iterate randomly over the internal table this might be one of the rare use cases of virtual table sorting 😉

      A bit more on topic: I'd always use the seed method instead of using sy-uzeit which is only precise to the second.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Good point. I didn't know about that method. So line 4 should be:

      DATA(random_generator) = cl_abap_random=>create( seed = cl_abap_random=>seed( ) ).
      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Dmitriy Sokoloff I'm genuinely curious how you found these FMs and why you felt this makes it a "comprehensive guide"?

      Even if we take the easy route of searching *RANDOM* in SE37, why not start with the same search in SE24? In this way, you would've found the class that Matthew Billingham suggested.

      There is definitely a problem in ABAP development that SAP doesn't tell us clearly what to use (there is no "library" concept and it's like Wild West in global classes) but still scratching my head on what is the reason you found only those 2 FMs.

      Thank you.

      Author's profile photo Mahmood Shakir Hammood
      Mahmood Shakir Hammood

      Hallo Dmitriy Sokoloff,

       

      do you have some blog about generate random data into the table with abap programming?