2012 Jul 30 8:27 AM
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?
2012 Jul 30 9:44 AM
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.
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.
2012 Jul 30 8:36 AM
2019 Jul 09 1:25 PM
2012 Jul 30 9:44 AM
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.
2012 Jul 30 10:26 AM
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
2012 Jul 30 10:33 AM
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
2012 Jul 30 10:45 AM
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.
2012 Jul 30 10:53 AM
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
2012 Jul 30 10:59 AM
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.
2012 Jul 30 11:07 AM
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
2012 Jul 30 11:08 AM
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
2012 Jul 30 11:13 AM
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
2012 Jul 30 11:26 AM
2012 Jul 30 12:27 PM
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
2012 Jul 30 12:42 PM
Yes, Suhas, I think that's a good illustration. And the uniform random distribution is granted for the sequence. Regards, Rüdiger
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |