This is a test message to test the length of the message box. 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.

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 ### ABAP Quick - Creation of test data

Category - ABAP

What challenges do you have when setting up test tables and test data and how can machine processing help you?

06/25/2021 ### ABAP Unit Quick Guide

Category - ABAP

Our current e-book is now available on Amazon, we would like to briefly show you what you can expect in the book and what it will bring you in the end.

05/28/2021 ### ABAP Quick - RFC Buffer

Category - ABAP

Here is a little tip from us about what can go wrong with the buffer when reading via RFC and what you should definitely pay attention to.

04/23/2021 ### 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