Pick random heights between 2 and 8. Try ranges of heights other than {2,8}—for example, {1,2}:
This draws a box with corners at coordinates {5,5,0} and {6,6,7}:
Graphics3D[
Cuboid[{5, 5, 0}, {6, 6, 7}],
PlotRange -> {{0, 11}, {0, 11}, {0, 11}}]
Another way to write the same thing is to separate the position of the box—{5,5,0}—from the size of the box—{1,1,7}. The first corner coordinate is given by the position, and the second by the position + size:
Graphics3D[
Cuboid[{5, 5, 0}, {5, 5, 0} + {1, 1, 7}],
PlotRange -> {{0, 11}, {0, 11}, {0, 11}}]
A better way to write the same thing is to use With so that if you want to change the box’s position, you only have to change the code in one place:
Graphics3D[
With[{pos = {5, 5, 0}}, Cuboid[pos, pos + {1, 1, 7}]],
PlotRange -> {{0, 11}, {0, 11}, {0, 11}}]
Now it’s easy to change the code to draw a height 7 box at a random position. Just replace pos={5,5,0} with pos=Append[RandomReal[10,2],0]. Run this code repeatedly to see the box move around:
Graphics3D[
With[{pos = Append[RandomReal[10, 2], 0]},
Cuboid[pos, pos + {1, 1, 7}]],
PlotRange -> {{0, 11}, {0, 11}, {0, 11}}]
Make the height vary between 2 and 8 by replacing 7 with RandomReal[{2,8}]:
Graphics3D[
With[{pos = Append[RandomReal[10, 2], 0]},
Cuboid[pos, pos + {1, 1, RandomReal[{2, 8}]}]],
PlotRange -> {{0, 11}, {0, 11}, {0, 11}}]
Use Table to draw 30 random boxes and remove PlotRange{{0,11},{0,11},{0,11}} so that the output is just big enough to contain the boxes:
Graphics3D[
Table[
With[{pos = Append[RandomReal[10, 2], 0]},
Cuboid[pos, pos + {1, 1, RandomReal[{2, 8}]}]],
{30}]
]
Graphics3D[
Table[With[{pos = Append[RandomReal[10, 2], 0]},
Cuboid[pos, pos + {1, 1, RandomReal[{2, 8}]}]], {30}]]