Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over 



[
1
,
…
,
20
]


{\displaystyle [1,\ldots ,20]}

.

The loops iterate rows and columns of the array printing the elements until the value 



20


{\displaystyle 20}

 is met.

Specifically, this task also shows how to break out of nested loops.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges


1 ss  [Seed of the random number generator.]sz
 
[*
 * lrx -- (number)
 * Push a random number from 1 to 20.
 *]sz
[
 [                [If preventing modulo bias:]sz
  sz               [Drop this random number.]sz
  lLx              [Loop.]sz
 ]SI
 [                [Loop:]sz
  [*
   * Formula (from POSIX) for random numbers of low quality.
   * Push a random number from 0 to 32767.
   *]sz
  ls 1103515245 * 12345 + 4294967296 % ss
  ls 65536 / 32768 %
 
  d 32768 20 % >I  [Prevent modulo bias.]sz
 ]d SL x
 20 % 1 +         [Be from 1 to 20.]sz
 LLsz LIsz        [Restore L, I.]sz
]sr
 
 
5 sb           [b = Total rows]sz
5 sc           [c = Total columns]sz
 
[Fill array a[] with random numbers from 1 to 20.]sz
[              [Inner loop for j:]sz
 lrx            [Push random number.]sz
 li lc * lj +   [Push index of a[i, j].]sz
 :a             [Put in a[].]sz
 lj 1 + d sj    [j += 1]sz
 lc >I          [Loop while c > j.]sz
]sI
[              [Outer loop for i:]sz
 0 d sj         [j = 0]sz
 lc >I          [Enter inner loop.]sz
 li 1 + d si    [i += 1]sz
 lb >L          [Loop while b > i.]sz
]sL
0 d si         [i = 0]sz
lb >L          [Enter outer loop.]sz
 
[Find a 20.]sz
[              [If detecting a 20:]sz
 li lj + 3 + Q  [Break outer loop.]sz
]sD
[              [Inner loop for j:]sz
 li lc * lj +   [Push index of a[i,j].]sz
 ;a             [Push value from a[].]sz
 p              [Print value and a newline.]sz
 20 =D          [Detect a 20.]sz
 lj 1 + d sj    [j += 1]sz
 lc >I          [Loop while c > j.]sz
]sI
[              [Outer loop for i:]sz
 0 d sj         [j = 0]sz
 lc >I          [Enter inner loop.]sz
 [==
]P              [Print "==" and a newline.]sz
 li 1 + d si    [i += 1]sz
 lb >L          [Loop while b > i.]sz
]sL
0 d si         [i = 0]sz
lb >L          [Enter outer loop.]sz