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 in Change

Category - ABAP

The programming language ABAP has been changing for years and is being modernized in various concepts. In this article, we'll look at it in detail.

06/24/2022 ### ABAP Quick - Clean Core

Category - ABAP

In this article something about Clean Core, what does it mean for the developer, what are the requirements and what do you have to pay attention to.

06/17/2022 ### ABAP Quick - Processing in a new task

Category - ABAP

This tip is about asynchronous processing in a new process and what to look out for.

01/07/2022 ### ABAP Quick - Convert JSON to internal

Category - ABAP

In this little tip, we'll go into how you can convert a JSON stream to an internal format and then use it properly.

12/10/2021 ### ABAP Quick - External currency to internal

Category - ABAP

A little tip to convert data from an Excel or CSV file into the correct internal currency format. A simple conversion can quickly lead to errors.

12/03/2021