Application Development 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: 

Global random integer generation in a range

Former Member
0 Kudos

Hello,

I need a way to achieve a global random integer generation in a range.

I tried FMs QF05_RANDOM_INTEGER and RANDOM_I4 without success because every time I call them from an object, always get the same number

For example, FM RANDOM_I4 with RND_MIN = 1 and RND_MAX = 5 gets RND_VALUE = 1 and FM QF05_RANDOM_INTEGER with RAN_INT_MAX = 5 and AN_INT_MIN = 1 gets RAN_INT = 3.

How can I get a global (with memory) random number generator?

Regards and thanks in advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hey run the code below, it will give you different numbers.


data RAN_INT like QF00-RAN_INT.
data G_RAN_SEED like QF00-RAN_SEED.


CALL FUNCTION 'QF05_RANDOM_INTEGER'
    EXPORTING
         RAN_INT_MAX   = 5
         RAN_INT_MIN   = 1
    IMPORTING
         RAN_INT       = RAN_INT
    EXCEPTIONS
         INVALID_INPUT = 1
         OTHERS        = 2
          .
IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


write ran_int.

G_RAN_SEED = ran_int.

CALL FUNCTION 'QF05_RANDOM_SAVE_SEED'
    EXPORTING
         RAN_SEED_NEXT = G_RAN_SEED
          .

4 REPLIES 4

Former Member
0 Kudos

Hi Alfredo,

You can create random number using the function module you mentioned or try the CL_ABAP_RANDOM_INT->GET_NEXT and store it in the SAP memory for your purpose.

I would recommend you to use GUID instead which are distributed over a range.

Regards,

George

Edited by: George Biswal on May 22, 2009 6:58 AM

Former Member
0 Kudos

Hey run the code below, it will give you different numbers.


data RAN_INT like QF00-RAN_INT.
data G_RAN_SEED like QF00-RAN_SEED.


CALL FUNCTION 'QF05_RANDOM_INTEGER'
    EXPORTING
         RAN_INT_MAX   = 5
         RAN_INT_MIN   = 1
    IMPORTING
         RAN_INT       = RAN_INT
    EXCEPTIONS
         INVALID_INPUT = 1
         OTHERS        = 2
          .
IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


write ran_int.

G_RAN_SEED = ran_int.

CALL FUNCTION 'QF05_RANDOM_SAVE_SEED'
    EXPORTING
         RAN_SEED_NEXT = G_RAN_SEED
          .

Former Member
0 Kudos

Well it seems the FM is not working in program, but works well in direct FM run. But the code below runs fine. Check it out


data RAN_SEED	LIKE	QF00-RAN_SEED.
data RAN_NUMBER	LIKE	QF00-RAN_NUMBER.

  while not ran_seed between 1 and 5.

    GET PARAMETER ID 'C_RAN_PARAMID' FIELD RAN_SEED.

    CALL FUNCTION 'QF05_RANDOM'
         EXPORTING
              RAN_SEED   = RAN_SEED
         IMPORTING
              RAN_NUMBER = RAN_NUMBER
              RAN_SEED   = RAN_SEED.

    SET PARAMETER ID 'C_RAN_PARAMID' FIELD RAN_SEED.

    ran_seed = ran_seed mod 10.

  endwhile.

  write:/ ran_seed.


Former Member
0 Kudos

Thanks to everyone. The step that I was missing was saving the seed to future reuse.

CALL FUNCTION 'QF05_RANDOM_SAVE_SEED'

EXPORTING

RAN_SEED_NEXT = G_RAN_SEED

.

Regards, Alfredo