Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Random number generation

Former Member
0 Likes
2,283

Hello

I want to generate random number in SAP. we use this random number as Outbound Delivery number i.e. LIKP-VBELN. We are not generating sequential number as the requirement is such that a person should not be able to predict the next delivery number for security purpose.

I thought of using fm QF05_RANDOM_INTEGER. It generates random number. But I am not sure if this FM will repeat the number as this can be used globally by any user. Kindly advice If I should use fm QF05_RANDOM_INTEGER.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,461

This will be really difficult to do. In addition to generating an random document number (easy), you also have to check that has not been used before. To do this you will have to read the database after generating the number and if it is already there, generate another. Keep doing this until you have a unique document number.

This won't take long at the start, but as the range fills up, it will become a performance problem.

Rob

7 REPLIES 7
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,461

>

> I want to generate random number in SAP. we use this random number as Outbound Delivery number i.e. LIKP-VBELN. We are not generating sequential number as the requirement is such that a person should not be able to predict the next delivery number for security purpose.

But if the number is going to be assigned to some document e.g., OB Delivery as you have mentioned, then i think we should respect the number range defined for the particular document.

BR,

Suhas

Read only

Former Member
0 Likes
1,461

Hi Suhas,

Thanks a lot for ur quick reply. But here the business requirement is that delivery number should be a random number. And using number range we get sequential number. Thus I am thinking of using Random Number generation logic. We would update this table in LIKP-VBELN in enhancement spot.

Do you know any other method to get random number for OB delivery.

Read only

Former Member
0 Likes
1,461

>

> We would update this table in LIKP-VBELN in enhancement spot.

I hope you are aware that the delivery number is also stored in several other database tables....

And regarding the requirement: i don't think i have ever come across such a weird one. Who cares what the delivery number is or will be? As long as unauthorized persons cannot enter your system i don;t see any reason why the next number should not be possible to guess.

Read only

brad_bohn
Active Contributor
0 Likes
1,461

There are several different classes or functions that can be used to generate random numbers, which you can find in the help files or by searching the browsers. You're never guaranteed uniqueness though, even when using GUIDs. But I agree with Suhas - it makes no sense at all to require that. I can't see any security scenario where the delivery number has to be unpredicatable. If you need a unique identifier for a delivery for some security purpose, I would use an additional field.

Read only

_IvanFemia_
Active Contributor
0 Likes
1,461

Hi,

Take a look to the class

CL_ABAP_RANDOM

Regards,

Ivan

Read only

Former Member
0 Likes
1,462

This will be really difficult to do. In addition to generating an random document number (easy), you also have to check that has not been used before. To do this you will have to read the database after generating the number and if it is already there, generate another. Keep doing this until you have a unique document number.

This won't take long at the start, but as the range fills up, it will become a performance problem.

Rob

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,461

Hello,

Please check the code snippet designed as per your requirement:

DATA: lcl_random TYPE REF TO cl_abap_random_int,
      v_from TYPE nrfrom,
      v_to TYPE nrto,
      v_min TYPE i,
      v_max TYPE i,
      v_random TYPE i.

* Get the Number Range details for PERNR
SELECT fromnumber tonumber UP TO 1 ROWS
  FROM nriv
  INTO (v_from, v_to)
  WHERE object = 'RP_PREL'
  AND nrrangenr = '02'.
ENDSELECT.

CHECK sy-subrc = 0.
MOVE: v_from TO v_min,
      v_to TO v_max.

* Get the Random Number generator class instance
TRY.
    lcl_random = cl_abap_random_int=>create( min = v_min
                                             max = v_max ).
  CATCH cx_abap_random.
ENDTRY.

DO.
*   Get the Random Number:
  v_random = lcl_random->get_next( ).

*   Check if the number is already used up
  SELECT COUNT( * ) FROM pa0003
    WHERE pernr = v_random.
  IF sy-dbcnt = 0.
    EXIT. "If no PERNR found, then use the number
  ELSE.
    CLEAR v_random.
  ENDIF.
ENDDO.

WRITE: / 'Random Personnel Number Generated:',
      40 v_random NO-GROUPING.

If you see the DO ... ENDDO block will be the one causing the pain As the Number Range gets filled the number of iterations will also increase.

Anyway hope this helps you a lil'bit

Cheers,

Suhas