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

read data randomly

Former Member
0 Likes
846

How can I read data records from an internal table in a random order?

My aim is to select records randomly from an internal table without speciafying a key.

4 REPLIES 4
Read only

Former Member
0 Likes
640

you can select based on the index.

Satish

Read only

Former Member
0 Likes
640

use RANDOM_AMOUNT to generate random number for index...

and read theline from itab corresponding to this index...

Read only

former_member189059
Active Contributor
0 Likes
640

Get the random number using the RANDOM_AMOUNT function into lv_number

then use the syntax

read table itab index lv_number.

also, make sure than your lv_number value is not greater than the number of rows in your internal table

Read only

former_member189059
Active Contributor
0 Likes
640

I think the function RANDOM_AMOUNT gives decimal numbers

Use this code to get only integers

*&---------------------------------------------------------------------*
*& Report  ZKRIS_RANDOM
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  zkris_random.

selection-screen begin of block b1 with frame title title1.
  selection-screen begin of block b2 with frame title title2.
    parameters: low type i.
    parameters: high type i.
  selection-screen end of block b2.
  parameters: num_vals type i.
selection-screen end of block b1.

initialization.
title1 = 'Parameters'.
title2 = 'Limits'.

start-of-selection.

  types: begin of random_numbers_type,
    random_nr type i,
    end of random_numbers_type.

  data: random_numbers type random_numbers_type  occurs 0 with header line.


  constants: a type i value 21,
             c type i value 101,
             m type i value 32768,
             start type i value 500.

  data: new_num type i,
        interval type i.

  data: last_num type i.

  if high < low or num_vals < 1.
    write: 'parameter error'.
    exit.
  endif.

  if last_num is initial.
    last_num = start.
  endif.

  clear random_numbers[].
  interval = high - low + 1.


  do num_vals times.
    new_num = ( a * last_num + c ) mod m.
    last_num = new_num.

    random_numbers-random_nr = ( new_num mod interval ) + low.
    append random_numbers.
  enddo.


  loop at random_numbers.
    write:/ random_numbers-random_nr.
  endloop.