Skip to main content

Randomly assigning color to Graphics3D objects?

I have a set of Graphics3D primitives (here, spheres) which I would like to assign either:

  1. One of a subset of colors, e.g. {Green, Red}
  2. A random color over some RGB interval

Writing something like:

Graphics3D[{RGBColor[RandomInteger[{0, 1}], RandomInteger[{0, 1}], RandomInteger[{0, 1}]], Sphere[{#[[1]], #[[2]], #[[3]]}, #[[4]]] & /@ SphereList... 

...appears to only assign one random color to all of the spheres.

I can force this to work by generating a string that explicitly specifies a random color for each sphere, but is there a simpler way to make this work?

Roger Harris
  • 1.5k
  • 1
  • 13
  • 20