Generate a heap of boxes (The text asks how many boxes there are in the picture)

Now you are required to generate such a problem and its answer.

Input


an integer n
(optionally) random source of two values, in case you don't want to use the default random.


Output can be


a 3D array A[n][n][n] of booleans, where A[i][j][k] =&gt; (i==0 || A[i-1][j][k]) &amp;&amp; (j==0 || A[i][j-1][k]) &amp;&amp; (k==0 || A[i][j][k-1]) (assuming 0-index)
where &amp;&amp; is logical AND, || is logical OR, and =&gt; is "implies" (if X is true, then Y is true).

Basically this says that if you have a box at some position then for each coordinate that coordinate must be either 0 or also have a box if that coordinate is one less. So no holes in any direction that gets you closer to the origin.
a 2D array B[n][n] of integers, where B[i][j] means how many k satisfy A[i][j][k]

Basically this is the height of the stacks where these heights can't become lower in any direction that gets you closer to the origin.
Besides the array, the amount of boxes (how many i,j,k satisfy A[i][j][k])


Every valid output should have nonzero probability to be outputted.

Example: (Here use 2D output format for convenience)

1 =&gt; [[0]] 0 or [[1]] 1
2 =&gt; [[0,0],[0,0]] 0 
     [[1,0],[0,0]] 1 
     [[1,0],[1,0]] 2 
     [[1,1],[0,0]] 2 
     [[1,1],[1,0]] 3 
     [[1,1],[1,1]] 4 
     [[2,0],[0,0]] 2 
     [[2,0],[1,0]] 3 
     [[2,0],[2,0]] 4 
     [[2,1],[0,0]] 3 
     [[2,1],[1,0]] 4 
     [[2,1],[1,1]] 5 
     [[2,1],[2,0]] 5 
     [[2,1],[2,1]] 6 
     [[2,2],[0,0]] 4 
     [[2,2],[1,0]] 5 
     [[2,2],[1,1]] 6 
     [[2,2],[2,0]] 6 
     [[2,2],[2,1]] 7 or
     [[2,2],[2,2]] 8


So for input 2 your program should randomly return one of the 20 possible outputs, and if run enough times every one of them should be returned at some point.

Your program must terminate.

Winning criteria

This is code-golf, shortest code in bytes win!
Perl 5, -ap 121 bytes / 56 bytes

This 121 byte solution has a uniform distribution over all valid arrangements of boxes. It tries to be somewhat efficient but still times out on TIO for size 4 or more



#!/usr/bin/perl -ap
@a=((0 x$_.$")x$_.$/)x$_."";s%0%/0(|.{@F}|(\X{@F}\X){@F})\G/+${$z="$`1$'"}++||push@a,$z;0%eg for@a;$_=$a[rand@a];$\=y/1//


Try it online!

If you don't care about uniformity it's of course easy to go shorter, e.g. this 56 bytes solution is very biased towards the origin (it probably gives more nice pictures for the motivating example though)

#!/usr/bin/perl -a
say map{\@$_[rand@;+1,$#{$_-1}];@;-$#$_.$"}@;for@;=1..$_


Try it online!

(checked for size 2 and 3 that it indeed generates all possible outputs)