Try now in the
Open Cloud »
(no sign-in required)

Random Spheres

Make random arrangements of 3D spheres (a good model for many materials).

Run the code to draw 20 random spheres in a size 5 region. Try more spheres:

SHOW/HIDE DETAILS

This draws a sphere at coordinate {0,0,0}:

In[1]:=
X
Out[1]=

Add PlotRange6 to make the drawing area extend 6 units in all directions, and AxesTrue to turn on axes so you can see the coordinate values. Rotate the graphic with the mouse to see it from all angles:

In[2]:=
X
Out[2]=

Use RandomReal to draw a sphere in a random position.

This gives a random number between 0 and 5. Every time you run the code, you get a different number:

In[3]:=
X
Out[3]=

This gives a list of three random numbers between 0 and 5. Every time you run the code, you get a different list:

In[4]:=
X
Out[4]=

Replace {0,0,0} with RandomReal[5,3] in the sphere drawing code to draw the sphere in a different position every time you run the code:

In[5]:=
X
Out[5]=

Use Table to draw lots of random spheres.

Table makes lists of things, like this list of 20 random numbers:

In[6]:=
X
Out[6]=

This uses Table to draw 20 spheres in random positions:

In[7]:=
X
Out[7]=

Turn off the axes by removing AxesTrue, and remove PlotRange6 too. Without an explicit PlotRange value, the plot range is calculated automatically to be just large enough to contain the contents of the graphic:

In[8]:=
X
Out[8]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Give the spheres 50% opacity. Try opacities other than .5:

SHOW/HIDE DETAILS

This draws an opaque sphere:

In[1]:=
X
Out[1]=

This draws a sphere that is 50% opaque (and thus 50% transparent):

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Make an interactive version. Drag the seed slider for different arrangements:

SHOW/HIDE DETAILS

Heres the code to draw 20 transparent spheres in random positions:

In[1]:=
X
Out[1]=

Use Manipulate to make the number of spheres interactive.

Wrap the expression with Manipulate[...], replace 30 with the variable number, and specify that number ranges from 5 to 200 in steps of 1. Drag the slider to change the number of spheres:

In[2]:=
X
Out[2]=

The spheres jump around as you drag the slider because RandomReal gives different coordinate positions each time.

If you add SeedRandom[12345] to the code, the sphere arrangement stays the same as you drag the slider. Thats because SeedRandom resets the random number generator to the same point each time the spheres are drawn, so that you get the same sequence of random numbers:

In[3]:=
X
Out[3]=

Let the random number seed vary from 10000 to 50000 in steps of 1 to select different sphere arrangements:

In[4]:=
X
Out[4]=

Add a control to set the opacity. Let the opacity vary from 0 to 1 with an initial value of 1:

In[5]:=
X
Out[5]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Use random colors for the spheres:

SHOW/HIDE DETAILS

RandomColor does just what youd expect. You get a different color each time you run the code:

In[1]:=
X
Out[1]=

Add RandomColor to the sphere code to color the spheres randomly:

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Allow random sphere sizes between 0 and 1.5. Try maximum sizes other than 1.5:

SHOW/HIDE DETAILS

Add a second value to Sphere to specify its radius. This sphere has a radius of 1:

In[1]:=
X
Out[1]=

Replace 1 with RandomReal[1.5] to get a different radius between 0 and 1.5 each time you run the code:

In[2]:=
X
Out[2]=

Make the spheres in the interactive code random sizes by specifying a radius of RandomReal[1.5] in Sphere:

In[3]:=
X
Out[3]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Constrain the range to be able to chop off the tops of spheres:

SHOW/HIDE DETAILS

By default, a graphic is made just large enough to accommodate its contents:

In[1]:=
X
Out[1]=

You can specify a plot range to limit a graphics extent. This graphic ranges from 0 to 3 in the vertical (z) direction. Spheres that cross the plot range boundaries are chopped open:

In[2]:=
X
Out[2]=

Add a plot range control to the interactive code so you can slice open the spheres:

In[3]:=
X
Out[3]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Share ItMake an interactive sphere website:

SHOW/HIDE DETAILS

Deploy the Manipulate to the Wolfram Cloud where anyone with a browser can use it:

In[1]:=
X
Out[1]=

Click the link in the output to visit the site.

Share the link by right-clicking it and choosing Copy Address. Paste the link into an email, tweet, or other message.

HIDE DETAILS
In[1]:=
X
Out[1]=