1
$\begingroup$

I would like to ask,how can we draw 50 spheres of radius 0.1 (randomly) in the unit cube.

This is my first try,

Graphics3D[
 Table[{Red, Sphere[RandomReal[{0.1, 0.9}, 3], 0.1]}, {50}], 
 PlotRange -> {0, 1}]

To define a unit cube is this enough???

$\endgroup$
  • $\begingroup$ Looks like this is a question from the quiz questions for the Wolfram-U "Programming Fundamentals Week 4" course here. $\endgroup$ – Rohit Namjoshi Jun 14 at 18:17
2
$\begingroup$

Your approach is fine.

A few alternatives you might consider:

  1. You can generate random centers using a single call to RandomReal:

Graphics3D[{Red, Sphere[RandomReal[{0.1, 0.9}, {50, 3}], 0.1]}, 
 PlotRange -> {0, 1}]

enter image description here

  1. You can transform Cuboid[] and use it with RandomPoint as follows:

r = .1;

transformedCuboid = Cuboid[r {1, 1, 1}, (1 - r) {1, 1, 1}];

centers = RandomPoint[transformedCuboid, 30];

Graphics3D[{PointSize[Medium], Point@centers, 
  Opacity[.5, Red], Sphere[#, r] & /@ centers, 
  EdgeForm[Blue], FaceForm[], Cuboid[], 
  EdgeForm[Green], transformedCuboid}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ what is the error in my proposed code? $\endgroup$ – ABCDEMMM Jun 14 at 17:54
  • $\begingroup$ can we fine the color inside the command "Sphere"? $\endgroup$ – ABCDEMMM Jun 14 at 18:01
  • 1
    $\begingroup$ @ABCDEMMM, I don't see any error in your code. $\endgroup$ – kglr Jun 14 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.