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

Function module to generate random number between 0 and 100

Former Member
0 Likes
7,376

Hi

My requirement is I need to generate random number between 0(zero) to 100(one hundred). My input criteria to the function module is 0 (zero) and 100 (One Hundred)

Kindly let me know the function module <removed_by_moderator>

I believe, I will get answer for my query.

Regards

Vijay

Edited by: Julius Bussche on Jul 11, 2008 1:16 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,610

You can use the FM 'RSSM_RANDOM_NUMBER_GET'.

Ypu can also use the FM 'ISH_RANDOM_NUMBER', pass '2' as exporting parameter.

Hi

My requirement is I need to generate random number between 0(zero) to 100(one hundred). My input criteria to the function module is 0 (zero) and 100 (One Hundred)

Kindly let me know the function module <removed_by_moderator>

I believe, I will get answer for my query.

Regards

Vijay

Edited by: Julius Bussche on Jul 11, 2008 1:16 PM

6 REPLIES 6
Read only

Former Member
0 Likes
2,611

You can use the FM 'RSSM_RANDOM_NUMBER_GET'.

Ypu can also use the FM 'ISH_RANDOM_NUMBER', pass '2' as exporting parameter.

Read only

0 Likes
2,610

Many thanks.

Read only

Former Member
2,610

Hiii!

The function module, QF05_RANDOM_INTEGER, can be used to generate a random number. Sample program using the function module is shown below:


 REPORT zrandom_test.

DATA:
w_num TYPE i.

CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
  ran_int_max = 1000
  ran_int_min = 1
IMPORTING
  ran_int = w_num
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 :/ 'Next available number is: ', w_num.

Regards

Abhijeet

Read only

0 Likes
2,610

good function~~

Read only

Former Member
0 Likes
2,610

Hi,

Welcome to SDN.

Check this code...

Here you need to input 2 numbers ie the upper limit and the lower limit. This works only for integers.


QF05_RANDOM_INTEGER
--------------------------------------------
Parameters: p_larg like QF00-RAN_INT,
            p_small like QF00-RAN_INT.

Data: d_result like QF00-RAN_INT.

CALL FUNCTION 'QF05_RANDOM_INTEGER'
 EXPORTING
   RAN_INT_MAX         = p_larg
   RAN_INT_MIN         = p_small
 IMPORTING
   RAN_INT             = d_result
 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:/ 'The Number is = ' , d_result.

OR


DATA RANINT LIKE QF00-RAN_INT.

PARAMETERS: P_MAX LIKE QF00-RAN_INT DEFAULT 100,

P_MIN LIKE QF00-RAN_INT DEFAULT 1,

P_TIMES TYPE I DEFAULT 10.

WRITE:/ 'RANDOM NUMBER'.

ULINE.

DO P_TIMES TIMES.

CALL FUNCTION 'QF05_RANDOM_INTEGER'
EXPORTING
  RAN_INT_MAX   = P_MAX
  RAN_INT_MIN   = P_MIN
IMPORTING
  RAN_INT       = RANINT
EXCEPTIONS
  INVALID_INPUT = 1
  OTHERS        = 2.

  IF SY-SUBRC EQ 0.
    WRITE:/ RANINT.
  ELSE.
    WRITE:/ 'UNABLE TO GENERATE RANDOM NUMBER'.
  ENDIF.
ENDDO.

OR

Check this link.....

http://www.saptechnical.com/Tips/ABAP/Random.htm

Hope this would solve your problem

Regards

Narin Nandivada

Read only

Former Member
0 Likes
2,610

Thanks a lot.