ABAP学习(30):生成随机数

ABAP生成随机数

使用cl_abap_random_int类生成整型随机数;

使用cl_abap_random_float类生成浮点随机数;

使用cl_abap_random_decfloat16类生成decfloat16类型随机数;

使用cl_abap_random_packed类生成P类型随机数;

使用cl_abap_random_packed_dec1-10类生成p类型decimal1-10随机数;

示例:

FORM f_test_random.
  DATA:lo_random_int TYPE REF TO cl_abap_random_int.
  DATA:lo_random_float TYPE REF TO cl_abap_random_float.
  DATA:lo_random_decfloat16 TYPE REF TO cl_abap_random_decfloat16.
  DATA:lo_random_decfloat34 TYPE REF TO cl_abap_random_decfloat34.
  DATA:lo_random_packed TYPE REF TO cl_abap_random_packed.
  "其他類cl_abap_random_packed_dec1~10,返回數據類型p,decimal1~10
  DATA:lv_int TYPE i.
  DATA:lv_float TYPE float.
  DATA:lv_dec16 TYPE decfloat16.
  DATA:lv_dec34 TYPE decfloat34.
  DATA:lv_packed TYPE p LENGTH 16 DECIMALS 0.
  "返回i
  lo_random_int = cl_abap_random_int=>create( seed = 1 min = 0 max = 1000 ).
  "返回f
  lo_random_float = cl_abap_random_float=>create( seed = 1 ).
  "返回DECFLOAT16
  lo_random_decfloat16 = cl_abap_random_decfloat16=>create( seed = 1 ).
  "返回DECFLOAT34
  lo_random_decfloat34 = cl_abap_random_decfloat34=>create( seed = 1 ).
  "返回p,decimal 0
  lo_random_packed = cl_abap_random_packed=>create( seed = 1 min = 1 max = 9999 ).
  DO 2000 TIMES.
    lv_int = lo_random_int->get_next( ).
    lv_float = lo_random_float->get_next( ).
    lv_dec16 = lo_random_decfloat16->get_next( ).
    lv_dec34 = lo_random_decfloat34->get_next( ).
    WRITE:/ lv_int,lv_float,lv_dec16,lv_dec34.
  ENDDO.
ENDFORM.
​

 

posted @ 2022-03-19 09:21  渔歌晚唱  阅读(626)  评论(0编辑  收藏  举报