This is a test message to test the length of the message box.
Login
ABAP Quick random numbers
Created by Software-Heroes

ABAP Quick - Random numbers

How do you create a neatly working random number generator in ABAP? We show you in this little article and what you should pay attention to.

Advertising

A random number generator in SAP seems rather lost, why should you need something like that, when everything is designed for logic? The answer is more for fun or for a test application based on random databases. The implementation is very simple, but you should pay attention to a few points to get a true random number generator.

 

Class cl_abap_random

The foundation is the class cl_abap_random, which can generate random numbers. To do this, create a simple instance using the factory method. With the different methods you can then generate different random numbers. In the example we have a simple cube that produces the numbers from 1 to 6.


" Create instance with factory
DATA(lo_rand) = cl_abap_random=>create( ).

" Random number from 1 till 6 (integer)
DATA(ld_num) = lo_rand->intinrange( low = 1 high = 6 ).

 

Less Random

Problem in the generation of random numbers is the chance itself. Because the generated numbers are not really very random in the order. For this we have the following experimental setup:


DO 15 TIMES.
  DATA(lo_rand) = cl_abap_random=>create( ).
  DO 30 TIMES.
    WRITE: CONV char1( lo_rand->intinrange( low = 1 high = 6 ) ).
  ENDDO.
  NEW-LINE.
ENDDO.

 

For each generated output line, the random number generator is instantiated again. The result is, however, that each row looks the same, the generated random numbers are repeated exactly in their order. However, this does not correspond to any coincidence, since the same result would be generated with each test case.

 

Solution

For this we need a little help to generate really random number series and that with each pass. The class's factory method picks up another parameter, the seed. This seed sets the starting value of the random generator. Working with a fixed start value/seed also produces the same result and is not recommended.

Therefore, at the beginning we need a "seed generator" that provides us random seeds for the random number generator. This should then also have been generated on a relatively random number. In our example, we use month/day, as well as the current time, to get the starting value. With this start value, we then generate a seed generator.


" Create first seed
TRY.
    " Seed with date and time
    DATA(ld_seed) = CONV i( |{ sy-datum(4) }{ sy-uzeit }| ).
  CATCH cx_sy_conversion_overflow.
    " Fallback seed number
    ld_seed = 1337.
ENDTRY.

" Seed generator
mo_seed = cl_abap_random=>create( ld_seed ).

 

The instance of this generator should be kept static or global so that it does not have to be recreated each time.

With the help of our seed generator, we can now create a class of cl_abap_random each time and provide it with a random seed number to really get even random numbers.


" Create instance
DATA(lo_rand) = cl_abap_random=>create( mo_seed->intinrange( low = 1 high = 999999 ) ).

 

Here's the complete example for creating the new cube result. Again 15 rows of numbers are created and each time a new instance of the random number generator is generated, this time only with the help of our seed generator.


" Create seed number
TRY.
    DATA(ld_seed) = CONV i( |{ sy-datum(4) }{ sy-uzeit }| ).
  CATCH cx_sy_conversion_overflow.
    ld_seed = 1337.
ENDTRY.
DATA(lo_seed) = cl_abap_random=>create( ld_seed ).

" Process logic
DO 15 TIMES.
  DATA(lo_rand) = cl_abap_random=>create( lo_seed->intinrange( low = 1 high = 999999 ) ).
  DO 30 TIMES.
    WRITE: CONV char1( lo_rand->intinrange( low = 1 high = 6 ) ).
  ENDDO.
  NEW-LINE.
ENDDO.

 

The current result is impressive and looks more like a coincidence than before. Here are 3 examples of what the program has generated in numbers and you will find that these are reasonably good looking numbers.

 

Conclusion

The implementation of a random number generator should not be a problem for you now, if you pay attention to the subtleties in the implementation. With the help of our example, it should no longer be a problem for you to implement an efficient solution.


Included topics:
QuickRandom
Comments (0)
Advertising

ABAP Quick - Wait for task

Category - ABAP

Today a tip for asynchronous processing or if you want to put processing in a separate task in special situations. What happens afterwards?

03/05/2021

ABAP Quick - Performance INSERT vs VALUE

Category - ABAP

In this article we take a look at the performance of the insert operations APPEND, INSERT and VALUE with regard to tables and evaluate the performance and stability during development.

02/26/2021

ABAP - BOPF Quick Guide (Update)

Category - ABAP

A little update to our book that we published. For all people who find the paperless office boring.

10/23/2020

ABAP - BOPF Quick Guide

Category - ABAP

You need a quick introduction to the SAP Business Object Processing Framework (BOPF), we have the right guide for you here.

09/11/2020

ABAP Quick - Search in source code

Category - ABAP

Sometimes it is necessary to search through different source codes in one system. We show you how it works in the SAP GUI and in Eclipse.

04/24/2020

We use cookies for our services and functions. More informations