on 09-23-2011 10:16 AM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.