How-to cast a set of random points on the surface of a cuboid? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-10-17T12:01:25Z https://mathematica.stackexchange.com/feeds/question/97783 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/97783 5 How-to cast a set of random points on the surface of a cuboid? e.doroskevic https://mathematica.stackexchange.com/users/18696 2015-10-24T22:57:23Z 2016-12-23T11:31:51Z <p>I have been able to implement point picking for cylinders and spheres. However, I struggle to implement a solution for a cuboid. </p> <p>Please see code for point generation on cylinders and spheres below: </p> <p><strong>Cylinder:</strong> </p> <pre><code> Point[Table[{radius*Cos[#1], radius*Sin[#1], #2} &amp;[ RandomReal[{0, 2 Pi}], RandomReal[{p1[], p2[]}]], {expNo}]]; </code></pre> <p><strong>Sphere:</strong></p> <pre><code> Point[Table[{Cos[#1] Sqrt[1 - #2^2], Sin[#1] Sqrt[1 - #2^2], #2} &amp;[ RandomReal[{0, 2 Pi}], RandomReal[{-radius, radius}]], {expNo}]]; </code></pre> <p>In both cases <code>{expNo}</code> denotes a number of points;</p> <p>How could I do the same for a <strong>Cube</strong>?</p> <p>I consulted <a href="http://mathworld.wolfram.com/CubePointPicking.html" rel="nofollow noreferrer">MathWorld</a> on how to do this, but I was unsuccessful in implementation.</p> https://mathematica.stackexchange.com/questions/97783/-/97788#97788 23 Answer by ilian for How-to cast a set of random points on the surface of a cuboid? ilian https://mathematica.stackexchange.com/users/145 2015-10-25T00:33:22Z 2015-10-25T15:08:50Z <p>Using <a href="http://reference.wolfram.com/language/ref/RandomPoint.html" rel="noreferrer"><code>RandomPoint</code></a> (available in <em>Mathematica</em> 10.2 or later):</p> <pre><code>c = Cuboid[]; pts = RandomPoint[RegionBoundary[c], 5000]; Graphics3D[Point[pts], Boxed -&gt; False] </code></pre> <p><img src="https://i.stack.imgur.com/nyK2U.png" alt="Mathematica graphics"></p> <p>Check the average distance to the centroid</p> <pre><code>Mean[Map[Norm[# - RegionCentroid[c]] &amp;, pts]] (* 0.640991 *) </code></pre> https://mathematica.stackexchange.com/questions/97783/-/97815#97815 4 Answer by J. M. will be back soon for How-to cast a set of random points on the surface of a cuboid? J. M. will be back soon https://mathematica.stackexchange.com/users/50 2015-10-25T14:01:10Z 2015-10-25T14:01:10Z <p>For people on older versions who cannot use <code>RandomPoint[]</code>, here is the method I was alluding to in the comments:</p> <pre><code>BlockRandom[SeedRandom[42, Method -&gt; "Rule30CA"]; (* for reproducibility *) n = 1*^4; (* number of points *) pmin = {3, 2, 1}; pmax = {7, 5, 3}; (* cuboid corners *) rif = Riffle[#, #] &amp;; (* utility function *) areas = Times @@@ Table[Delete[pmax - pmin, k], {k, 3}]; facs = Table[Drop[{pmin, pmax}, None, {k}], {k, 3}]; pts = With[{zl = (#1 + I #2) &amp; @@@ #1}, Insert[Through[{Re, Im}[RandomComplex[zl]]], #2, #3]] &amp; @@@ RandomChoice[rif[areas] -&gt; Transpose[{ rif[facs], Flatten[Transpose[{pmin, pmax}]], rif[Range]}], n]; Graphics3D[{{Directive[FaceForm[], EdgeForm[Directive[AbsoluteThickness, Red]]], Cuboid[pmin, pmax]}, {Directive[AbsolutePointSize, Blue], Point[pts]}}, Boxed -&gt; False]] </code></pre> <p><img src="https://i.stack.imgur.com/iO1Qy.png" alt="random points on cuboid surface"></p>