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

Random number using CL_ABAP_RANDOM_INT

TimoScharmann
Participant
0 Likes
11,191

Hi experts,

I am trying to get a random number using the class CL_ABAP_RANDOM_INT, but the following code always return 22.

DATA:
   lo_ran TYPE REF TO CL_ABAP_RANDOM_INT,
   lv_i   TYPE i.

lo_ran = CL_ABAP_RANDOM_INT=>CREATE( MIN = 5 MAX = 25 ).
lv_i = lo_ran->GET_NEXT( ).

WRITE lv_i.

Do you know, how to get random numbers using this class?

1 ACCEPTED SOLUTION
Read only

Ruediger_Plantiko
Active Contributor
5,611

You will need a seed value at the initialization, to start with different start values. The instance will then produce a series of numbers which are approximately uniformly distributed.

report  zz_random_int.
data:
   lo_ran type ref to cl_abap_random_int,
   lv_i   type i,
   lv_seed type i.

lv_seed = sy-timlo.
lo_ran = cl_abap_random_int=>create( min = 5 max = 25 seed = lv_seed ).
do 20 times.
  lv_i = lo_ran->get_next( ).
  write / lv_i.
enddo.

Hi experts,

I am trying to get a random number using the class CL_ABAP_RANDOM_INT, but the following code always return 22.

DATA:
   lo_ran TYPE REF TO CL_ABAP_RANDOM_INT,
   lv_i   TYPE i.

lo_ran = CL_ABAP_RANDOM_INT=>CREATE( MIN = 5 MAX = 25 ).
lv_i = lo_ran->GET_NEXT( ).

WRITE lv_i.

Do you know, how to get random numbers using this class?

14 REPLIES 14
Read only

Aashish28
Contributor
5,611

Hiii ,

            Use this FM - SXMS_CREATE_RANDOM_NUMBER.

Read only

0 Likes
5,611

Hi dear Ashish.

You are absolutely right !!!.

Kind regards,

Read only

Ruediger_Plantiko
Active Contributor
5,612

You will need a seed value at the initialization, to start with different start values. The instance will then produce a series of numbers which are approximately uniformly distributed.

report  zz_random_int.
data:
   lo_ran type ref to cl_abap_random_int,
   lv_i   type i,
   lv_seed type i.

lv_seed = sy-timlo.
lo_ran = cl_abap_random_int=>create( min = 5 max = 25 seed = lv_seed ).
do 20 times.
  lv_i = lo_ran->get_next( ).
  write / lv_i.
enddo.

Read only

0 Likes
5,611

I tried with and without using a seed value.

When you are using a loop and calling GET_NEXT in that it gives you Random numbers even without Seed value.

Thanks,

Shambu

Read only

0 Likes
5,611

I repeat. This is the way a pseudo random number generator is designed to work. No problem here!

You may imagine it as an infinite series of randomly distributed numbers. With the seed, an entry point into this series is determined (by an algorithm). With successive calls of get_next, you read successive numbers from the list, starting with that entry point (actually, they are determined by an algorithm too, but it is helpful to visualize it as a list).

http://en.wikipedia.org/wiki/Pseudorandom_number_generator

Regards,

Rüdiger

Read only

0 Likes
5,611

Thanks, I overlooked the below part.

lv_seed = sy-timlo.

This will give you random numbers unless you run it the same time every day.

Read only

0 Likes
5,611

Still don't agree. The start value is not "random", even when starting with different seed values. Only the sequence that is generated from then on, is guaranteed to be uniformly distributed (within certain bounds).

Regards, Rüdiger

Read only

0 Likes
5,611

I didnt say the start value will be random, what I said was if I execute the below statement

lo_ran = cl_abap_random_int=>create( min = 5 max = 25 seed = 10 ).  

lv_i = lo_ran->get_next( ).  

write / lv_i.  

lv_i will be 21 always. So similarly if you feed the same time to the CREATE method, it will give you the same value always.

Read only

0 Likes
5,611

Hi,

How if start value will be random, this code may be a generic solution?

DATA:

    lo_ran TYPE REF TO CL_ABAP_RANDOM_INT,

    lv_i   TYPE i.

  lv_i = CL_ABAP_RANDOM=>SEED( ).

  lo_ran = CL_ABAP_RANDOM_INT=>CREATE( seed = lv_i  MIN = 5 MAX = 25 ).

do 20 times.

   lv_i = lo_ran->get_next( ).

   write / lv_i.

enddo.

Thanks,

Sharath

Read only

0 Likes
5,611

Dear Shambu,

OK, then I misunderstood you.  From this statement,

This will give you random numbers unless you run it the same time every day.

I thought that you intended to generate just one single random number from the PRG. Next day, another number, and so on, and then assumed that these numbers were "random" in some sense.

But the PRG doesn't assert that you get a sequence of random numbers when starting it with different seed values. (This will depend on the distribution of your seed values). It only asserts that a sequence of successive calls of get_next( ) will be uniformly distributed.

Sorry for being so scrupulous here...

Regards,

Rüdiger

Read only

0 Likes
5,611

Hi Sharath,

this is what the docu of cl_abap_random=>seed( ) says:

Calculates an arbitrary start value for a pseudo-random generator from

different user data (such as time or user ID).

This is therefore similar to my sy-timlo solution. I only like to stress that the real "randomness" of all the CL_ABAP_RANDOM subclasses lies not in the start value, but in the series.

Regards,

Rüdiger

Read only

0 Likes
5,611

Thanks Rüdiger! This works fine!

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
5,611

Hello Rudiger et al.,

If i understand correctly the Seed is analogous to the PIN which we input into the secure token (e.g., the one we use to login to Bank portal) & it gives back a randomly generated number.

And based on the pre-defined time interval a new number is generated (similar to the random numbers generated inside DO .. ENDDO iteration).

Cheers,

Suhas

Read only

0 Likes
5,611

Yes, Suhas, I think that's a good illustration. And the uniform random distribution is granted for the sequence. Regards, Rüdiger