Create a random arrangement of “skyscrapers”.
Run the code to place 30 random squares in a size-10 region. Try numbers other than 30:
This draws a default rectangle, which is a square:
Add PlotRange{{0,10},{0,10}} to make the drawing area extend from 0 to 10 in both directions, and AxesTrue to turn on axes so you can see the coordinate values:
You can draw a square in a random position using RandomReal.
This gives a random number between 0 and 10. Each time you run the code, you get a different number:
This gives a pair of random numbers between 0 and 10. Each time you run the code, you get a different pair:
Use RandomReal to draw a square in a random position. Each time you run the code, the square is in a different place:
Table makes lists of things. This uses Table to make a list of 5 random numbers:
This uses Table to draw 30 squares in random positions:
Turn off the axes by removing AxesTrue, and remove PlotRange{{0,10},{0,10}} too. Without an explicit PlotRange value, the plot range is calculated automatically to be just large enough to contain the contents of the graphic:
Use 3D cuboids instead of 2D squares. Try numbers other than 30:
This draws a square at {0,0}:
This draws a cube at {0,0,0}. The expression for drawing a cube is like the one for drawing a square, but Graphics is replaced by Graphics3D, Rectangle by Cuboid and the two-dimensional coordinate {0,0} by the three-dimensional coordinate {0,0,0}:
You can draw cubes in random positions similar to the way you drew squares in random positions. In addition to replacing Graphics with Graphics3D and Rectangle with Cuboid, you need to replace RandomReal[10,2] with RandomReal[10,3] so that the coordinate values are three-dimensional:
To draw cubes in random positions but with the same vertical coordinate, make a two-dimensional coordinate with RandomReal[10,2] and append zero to it:
Use that code to make random cubes that all lie in the same vertical position:
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}:
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:
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:
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:
Make the height vary between 2 and 8 by replacing 7 with RandomReal[{2,8}]:
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:
Use neutral lighting (and remove the box):
Use BoxedFalse to draw the graphic without a box around it:
Add Lighting"Neutral" to get white lighting:
Use random gray levels between .3 and .7 for the “skyscrapers”. Try ranges of gray levels other than {.3,.7}—for example, {0,1}:
This draws a white box:
This draws a 50% gray box:
This draws a box with a random gray color between 30% and 70%. Each time you run the code, you get a different gray:
Add GrayLevel[RandomReal[{.3,.7}]] to the “skyscraper” code to get randomly colored buildings:
Add ground for the skyscrapers to stand on. Try colors other than Brown—for example, Pink or Darker[Green]:
Add another Cuboid to represent the ground. The skyscrapers are positioned between 0 and 10, and they are 1 unit wide and deep, so their footprints will be anywhere between 0 to 11 in the x and y directions. Add a 1-unit margin around the edges for a range of -1 to 12.
The bottoms of the skyscrapers are at z coordinate 0. Make the ground cuboid .5 units thick by making it extend downward to -.5. That gives Cuboid[{{-1,-1,-.5},{12,12,0}}] for the ground:
Color the ground Brown:
Interactively fill in skyscrapers. Drag the slider to fill in the landscape:
This is the code to make a landscape with 30 skyscrapers:
You can make the landscape interactive with Manipulate.
Manipulate attaches interactive controls to things like this expression for 3 squared:
Make that expression interactive by wrapping it with Manipulate[...], replacing the 3 with n and specifying that n goes from 0 to 30 in steps of 1. Drag the slider to see the squares of various numbers:
You can do the same thing with the skyscraper expression to make it interactive. Wrap it with Manipulate[...], replace the 30 with n and specify that n goes from 0 to 30 in steps of 1. Drag the slider to see the number of buildings change:
The buildings jump around because RandomReal gives different numbers each time it is used. You can make them stay put by adding SeedRandom[12345] to the code:
Here’s why SeedRandom[12345] keeps the buildings from jumping around.
RandomReal gives a different number each time it is used, so each time a new landscape is drawn, the buildings are in different positions.
SeedRandom[12345] resets the random number generator so that subsequent uses of RandomReal give the same sequence of random numbers.
This list of random numbers changes each time you run the code:
This list of random numbers is always the same because the random number generator is reset with SeedRandom:
You can give different numbers to SeedRandom to reset the random number generator to different places.
Share It—make an interactive cityscapes website:
Deploy the Manipulate to the Wolfram Cloud, where anyone with a browser can use it:
Click the link in the output to visit the site.
Tell the world about your creation by sharing the link via email, tweet or other message.