3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo "Задача: написать random3() и random4() возвращающие равновероятные значения используя только random2()\n"; echo random2() . " - random 0|1\n"; echo random3() . " - random 0|1|2\n"; echo random4() . " - random 0|1|2|3\n\n"; function random2(): int { return random_int(0, 1); } /** * @return 0|1|2|3 */ function random4(): int { return random2() + 2 * random2(); } /** * @return 0|1|2 */ function random3(): int { $x = rr3x3(); return 1 + ("{$x[0]}{$x[1]}{$x[2]}" % 3); } //1,1,1 //1,1,2 //1,1,3 //всегда полная равнораспределенная комбинация 1-2-3 в каждом регистре function rr3x3(): array { $x1 = 1 + random2() + random2(); $x2 = 1 + random2() + random2(); $x3 = 1 + random2() + random2(); if ($x2 === 2) { $x2 = 1; } elseif ($x2 === 3) { $x2 = 2; } if ($x3 === 2) { $x3 = 3; } elseif ($x3 === 3) { $x3 = 2; } return [$x1, $x2, $x3]; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXpL
function name:  (null)
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ECHO                                                     '%D0%97%D0%B0%D0%B4%D0%B0%D1%87%D0%B0%3A+%D0%BD%D0%B0%D0%BF%D0%B8%D1%81%D0%B0%D1%82%D1%8C+random3%28%29+%D0%B8+random4%28%29+%D0%B2%D0%BE%D0%B7%D0%B2%D1%80%D0%B0%D1%89%D0%B0%D1%8E%D1%89%D0%B8%D0%B5+%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%D0%B2%D0%B5%D1%80%D0%BE%D1%8F%D1%82%D0%BD%D1%8B%D0%B5+%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D1%8F+%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D1%83%D1%8F+%D1%82%D0%BE%D0%BB%D1%8C%D0%BA%D0%BE+random2%28%29%0A'
    4     1        INIT_FCALL_BY_NAME                                       'random2'
          2        DO_FCALL                                      0  $0      
          3        CONCAT                                           ~1      $0, '+-+random+0%7C1%0A'
          4        ECHO                                                     ~1
    5     5        INIT_FCALL_BY_NAME                                       'random3'
          6        DO_FCALL                                      0  $2      
          7        CONCAT                                           ~3      $2, '+-+random+0%7C1%7C2%0A'
          8        ECHO                                                     ~3
    6     9        INIT_FCALL_BY_NAME                                       'random4'
         10        DO_FCALL                                      0  $4      
         11        CONCAT                                           ~5      $4, '+-+random+0%7C1%7C2%7C3%0A%0A'
         12        ECHO                                                     ~5
   53    13      > RETURN                                                   1

Function random2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXpL
function name:  random2
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   INIT_FCALL                                               'random_int'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 1
          3        DO_ICALL                                         $0      
          4        VERIFY_RETURN_TYPE                                       $0
          5      > RETURN                                                   $0
   11     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function random2

Function random4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXpL
function name:  random4
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   INIT_FCALL                                               'random2'
          1        DO_FCALL                                      0  $0      
          2        INIT_FCALL                                               'random2'
          3        DO_FCALL                                      0  $1      
          4        MUL                                              ~2      $1, 2
          5        ADD                                              ~3      $0, ~2
          6        VERIFY_RETURN_TYPE                                       ~3
          7      > RETURN                                                   ~3
   20     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function random4

Function random3:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/GBXpL
function name:  random3
number of ops:  15
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   INIT_FCALL_BY_NAME                                       'rr3x3'
          1        DO_FCALL                                      0  $1      
          2        ASSIGN                                                   !0, $1
   28     3        FETCH_DIM_R                                      ~3      !0, 0
          4        ROPE_INIT                                     3  ~7      ~3
          5        FETCH_DIM_R                                      ~4      !0, 1
          6        ROPE_ADD                                      1  ~7      ~7, ~4
          7        FETCH_DIM_R                                      ~5      !0, 2
          8        ROPE_END                                      2  ~6      ~7, ~5
          9        MOD                                              ~9      ~6, 3
         10        ADD                                              ~10     1, ~9
         11        VERIFY_RETURN_TYPE                                       ~10
         12      > RETURN                                                   ~10
   29    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function random3

Function rr3x3:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 35
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
Branch analysis from position: 32
Branch analysis from position: 28
filename:       /in/GBXpL
function name:  rr3x3
number of ops:  42
compiled vars:  !0 = $x1, !1 = $x2, !2 = $x3
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   INIT_FCALL                                               'random2'
          1        DO_FCALL                                      0  $3      
          2        ADD                                              ~4      1, $3
          3        INIT_FCALL                                               'random2'
          4        DO_FCALL                                      0  $5      
          5        ADD                                              ~6      ~4, $5
          6        ASSIGN                                                   !0, ~6
   38     7        INIT_FCALL                                               'random2'
          8        DO_FCALL                                      0  $8      
          9        ADD                                              ~9      1, $8
         10        INIT_FCALL                                               'random2'
         11        DO_FCALL                                      0  $10     
         12        ADD                                              ~11     ~9, $10
         13        ASSIGN                                                   !1, ~11
   39    14        INIT_FCALL                                               'random2'
         15        DO_FCALL                                      0  $13     
         16        ADD                                              ~14     1, $13
         17        INIT_FCALL                                               'random2'
         18        DO_FCALL                                      0  $15     
         19        ADD                                              ~16     ~14, $15
         20        ASSIGN                                                   !2, ~16
   41    21        IS_IDENTICAL                                             !1, 2
         22      > JMPZ                                                     ~18, ->25
   42    23    >   ASSIGN                                                   !1, 1
   41    24      > JMP                                                      ->28
   43    25    >   IS_IDENTICAL                                             !1, 3
         26      > JMPZ                                                     ~20, ->28
   44    27    >   ASSIGN                                                   !1, 2
   46    28    >   IS_IDENTICAL                                             !2, 2
         29      > JMPZ                                                     ~22, ->32
   47    30    >   ASSIGN                                                   !2, 3
   46    31      > JMP                                                      ->35
   48    32    >   IS_IDENTICAL                                             !2, 3
         33      > JMPZ                                                     ~24, ->35
   49    34    >   ASSIGN                                                   !2, 2
   52    35    >   INIT_ARRAY                                       ~26     !0
         36        ADD_ARRAY_ELEMENT                                ~26     !1
         37        ADD_ARRAY_ELEMENT                                ~26     !2
         38        VERIFY_RETURN_TYPE                                       ~26
         39      > RETURN                                                   ~26
   53    40*       VERIFY_RETURN_TYPE                                       
         41*     > RETURN                                                   null

End of function rr3x3

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
195.92 ms | 1011 KiB | 22 Q