5 better explanation
source | link
                      ?29   take a random natural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ (the function argument) spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
                        .  then
             ?28            .  take a random natural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵<⍵ (see explanation below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/     {                     finally ignore the numeric result of the reduction

The reduction (right-fold) of a replicationreplicated value {...}/a/b is just a trick I came up with to repeat a function a-1 times, starting with value b and having the result of each iteration be the accumulator () argument ofto the next. The second and next input arguments () are ignored, as iswell as the final result are ignored. It turns out to be way shorter than a regular recursive call with guard.

                      ?29   take a random natural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ (the function argument) spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
                        .  then
             ?28            .  take a random natural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see explanation below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction

The reduction of a replication {...}/a/b is just a trick I came up with to repeat a function a-1 times, starting with value b and having the result of each iteration be the accumulator () argument of the next. The second and next input arguments () are ignored, as is the final result. It turns out to be way shorter than a regular recursive call with guard.

                   ?29  take a random natural from 1 to 29
                31/     repeat it 31 times
              }/        reduce (right-fold) the list using the given function:
          ⍵↑''          . make a string of ⍵ (the function argument) spaces
     '.',⍨              . append a dot to its right
⎕←30↑                   . right-pad it with spaces up to length 30 and output
                       . then
             ?28        . take a random natural from 1 to 28
          .5+           . add 0.5, giving a number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>              . check whether the result is <⍵ (see explanation below)
     ¯1*                . raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                   . add it to ⍵ and return it as the new accumulator value
0/{                     finally ignore the numeric result of the reduction

The reduction (right-fold) of a replicated value {...}/a/b is just a trick I came up with to repeat a function a-1 times, starting with value b and having the result of each iteration be the accumulator () argument to the next. The second and next input arguments () as well as the final result are ignored. It turns out to be way shorter than a regular recursive call with guard.

4 better explanation
source | link
                      ?29   take a random natural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ (the function argument) spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
   ◇                        .  then
             ?28            .  take a random natural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see explanation below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction

WhenAt every step, this code decides whether to move the currentdot to the left or to the right depending on the probability that a random number chosen among (1.5 2.5 ... 27.5 28.5) is less than the current dot position.

Therefore, when the current dot position (number of spaces to the left) is 1 the increment is certainlyalways +1 (all of those numbers 1.5 ... 28.5 are > 1), when it's 29 it's certainlyalways -1;1 (all of those numbers are < 29); otherwise it's chosen randomly between +1 and -1, with a probability that's a linear interpolation between those extremes. ThereforeSo the dot is always moving and always more likely to move towards the center than towards the sides. If it's exactly in the middle, it has a 50% chance to move to either side.

The reduction of a replication {...}/a/b is just a trick I came up with to repeat some codea function a-1 times, starting with value b and having the result of each iteration be the rightaccumulator () argument of the next. The leftsecond and next input arguments () are ignored, as is the final result. It turns out to be way shorter than a regular recursive call with guard.

                      ?29   take a random natural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
   ◇                        .  then
             ?28            .  take a random natural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction

When the current number of spaces is 1 the increment is certainly +1, when it's 29 it's certainly -1; otherwise it's chosen randomly between +1 and -1, with a probability that's a linear interpolation between those extremes. Therefore the dot is always moving and always more likely to move towards the center than towards the sides. If it's exactly in the middle, it has a 50% chance to move to either side.

The reduction of a replication {...}/a/b is just a trick I came up with to repeat some code a-1 times, starting with value b and having the result of each iteration be the right argument of the next. The left arguments are ignored, as is the final result. It turns out to be way shorter than a regular recursive call with guard.

                      ?29   take a random natural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ (the function argument) spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
   ◇                        .  then
             ?28            .  take a random natural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see explanation below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction

At every step, this code decides whether to move the dot to the left or to the right depending on the probability that a random number chosen among (1.5 2.5 ... 27.5 28.5) is less than the current dot position.

Therefore, when the current dot position (number of spaces to the left) is 1 the increment is always +1 (all of those numbers 1.5 ... 28.5 are > 1), when it's 29 it's always -1 (all of those numbers are < 29); otherwise it's chosen randomly between +1 and -1, with a probability that's a linear interpolation between those extremes. So the dot is always moving and always more likely to move towards the center than towards the sides. If it's exactly in the middle, it has a 50% chance to move to either side.

The reduction of a replication {...}/a/b is just a trick I came up with to repeat a function a-1 times, starting with value b and having the result of each iteration be the accumulator () argument of the next. The second and next input arguments () are ignored, as is the final result. It turns out to be way shorter than a regular recursive call with guard.

3 added 2 characters in body
source | link
                      ?29   take a random numbernatural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
   ◇                        .  then
             ?28            .  take a random numbernatural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction
                      ?29   take a random number from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
   ◇                        .  then
             ?28            .  take a random number from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction
                      ?29   take a random natural from 1 to 29
                   31/      repeat it 31 times
  {              }/         reduce (right-fold) the list using the given function:
             ⍵↑''           .  make a string of ⍵ spaces
        '.',⍨               .  append a dot to its right
   ⎕←30↑                    .  right-pad it with spaces up to length 30 and output
   ◇                        .  then
             ?28            .  take a random natural from 1 to 28
          .5+               .  add 0.5, giving a random number ∊ (1.5 2.5 ... 27.5 28.5)
        ⍵>                  .  check whether the result is < ⍵ (see below)
     ¯1*                    .  raise -1 to the boolean result (0 1 become resp. 1 -1)
   ⍵+                       .  add it to ⍵ and return it as the new accumulator value
0/                          finally ignore the numeric result of the reduction
2 added 73 characters in body
source | link
1
source | link