cancel
Showing results for 
Search instead for 
Did you mean: 

Need functionality to pick any 3 random questions from 5 input fields

Former Member
0 Kudos

Hi Experts,

In our password reset web dynpro application, we are providing 5 security questions to be answered by users,

but can we have the option to give only any 3 input fields randomly from 5 and allowing to be filled.

Need functionality to pick 3 random questions from 5.

Thanks in Advance

Sateesh J

Accepted Solutions (0)

Answers (1)

Answers (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

ABAP has a random number generator: CL_ABAP_RANDOM. You could assign each question a number and then call the random generator 3 times and see what you get back. Of course you will have to account for the possibility of generating the same number more than once.

Here is example of using this class to read a random record within an internal table. Something similiar could be used by you in this situation.

select filename into corresponding fields of table lt_imgs from zteched_img.
    read table lt_imgs into ls_imgs index cl_abap_random=>create( cl_abap_random=>seed( ) )->intinrange(
         low   = 1
         high  = lines( lt_imgs )  ).
    lv_filename = ls_imgs-filename.

Former Member
0 Kudos

Hi Thomas,

Thanks for your reply. i am getting the below error when executed the code above.

Unable to interpret "CL_ABAP_RANDOM=>SEED(". Possible causes: Incorrect

spelling or comma error.

Thanks

Sateesh

ChandraMahajan
Active Contributor
0 Kudos

Hi,

Use below sample code to implement the required functionality.

DATA: lt_imgs TYPE STANDARD TABLE OF mara,

ls_imgs LIKE LINE OF lt_imgs,

ld_rnd_index TYPE i,

ld_rnd_lines TYPE i,

lv_matnr TYPE matnr.

DATA:

sd_seed TYPE i,

so_random TYPE REF TO cl_abap_random.

IF so_random IS NOT BOUND.

so_random = cl_abap_random=>create( seed = sd_seed ).

ENDIF.

*get the data

SELECT matnr INTO CORRESPONDING FIELDS OF TABLE lt_imgs FROM mara UP TO 10 rows.

*read the no of lines from internal table

ld_rnd_lines = LINES( lt_imgs ).

*we will get the random number from this low and high range

ld_rnd_index = so_random->intinrange( low = 1 high = ld_rnd_lines ).

*read the internal table with random number

READ TABLE lt_imgs INTO ls_imgs INDEX ld_rnd_index.

lv_matnr = ls_imgs-matnr. "Random Material

Thanks,

Chandra

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

What release level are you on? My sample is from 7.02 and therefore I use statement chaning based syntax. If you are on an older release level you will have to break out the chaining. However the classes referenced all still work.

Former Member
0 Kudos

Hi Chandra,

Thanks for your inputs.

I have to get 3 random questions out of 5 -- mean 3 random numbers.

so i am executing the code for 3 times.. Using DO 3 TIMES, followed by case statement.

But i could not able to get 3 random numbers correctly sometimes it generates only 2 random numbes and sometime 3.

Not sure where i am missing.

Thanks

Sateesh

ChandraMahajan
Active Contributor
0 Kudos

Hi,

*we will get the random number from this low and high range

ld_rnd_index = so_random->intinrange( low = 1 high = ld_rnd_lines ).

your range should be low = 1 and High = 5. this will give you any random number from 1 to 5. I hope you will be able to code accordingly.

Thanks,

Chandra

Former Member
0 Kudos

Hi Chandra,

ld_rnd_index = so_random->intinrange( low = 1 high = ld_rnd_lines )is reading 3 always.

any update.

Thanks

Sateesh J

ChandraMahajan
Active Contributor
0 Kudos

Hi,

that was just sample code.

you can use code like below.

ld_rnd_index = so_random->intinrange( low = 1 high = 5 ).

your low and high are 1 to 5.

Thanks,

Chandra

Former Member
0 Kudos

Chandra,

for 3 times it repeatedly generating the same numbers. no random functionality working.

Thanks

Sateesh

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

You will get the same number with this sample. You need to create the random object with a new seed each time.

ChandraMahajan
Active Contributor
0 Kudos

Hi,

modify the sample code accordingly or else use below code.

DATA :

lv_mini TYPE i,

lv_maxi TYPE i,

lv_rndi TYPE i,

lv_i TYPE i.

lv_mini = 1.

lv_maxi = 5.

lv_i = 3.

WHILE lv_i > 0.

CALL FUNCTION 'QF05_RANDOM_INTEGER'

EXPORTING

ran_int_max = lv_maxi

ran_int_min = lv_mini

IMPORTING

ran_int = lv_rndi

EXCEPTIONS

invalid_input = 1

OTHERS = 2.

CASE lv_rndi.

WHEN 1.

WRITE 😕 'Question - ',lv_rndi.

WHEN 2.

WRITE 😕 'Question - ',lv_rndi.

WHEN 3.

WRITE 😕 'Question - ',lv_rndi.

WHEN 4.

WRITE 😕 'Question - ',lv_rndi.

WHEN 5.

WRITE 😕 'Question - ',lv_rndi.

WHEN OTHERS.

ENDCASE.

lv_i = lv_i - 1.

ENDWHILE.

Thanks,

Chandra

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

QF05_RANDOM_INTEGER is an unreleased function module and therefore not supported for customer or partner usage. You should instead use the suggested Random ABAP classes. They are supported and use the ABAP kernel.