How do I create a random point on the surface of a cube? I mean on one of its six faces at random.
Asked
Active
Viewed 745 times
4
3 Answers
5
We simply need a MeshRegion that represents the boundary of the unit cube. Then we apply RandomPoint to it. The following shows one of probably many ways to do it.
R = DiscretizeRegion@RegionBoundary[Cuboid[]];
RandomPoint[R]
Henrik Schumacher
- 106,770
- 7
- 179
- 309
-
Sorry, not into regions lately, will
RegionBoundarybe ok too?RandomPoint[DiscretizeRegion@RegionBoundary@Cuboid[], 500]– Kuba Aug 10 '18 at 15:19 -
@Kuba Ah right! I tried all combinations of
MeshandBoundarybut forget aboutRegionBoundary! Thank's for the hint. – Henrik Schumacher Aug 10 '18 at 15:26 -
-
DiscretizeRegionshould really be redundant on this. ConsiderRandomPoint@RegionBoundary@Ball[]. Once again, for some reason it's necessary. – kirma Aug 10 '18 at 18:17 -
@kirma That's because
RegionBoundary@Ball[]reduces toSphere[{0, 0, 0}]and this one has its own branch inRandomPoint. But there isn't such an implementation forPolygons which are not simplices and that's actually for a good reason: These polygons can be nonplanar and it is not trivial to tell what their area should be. – Henrik Schumacher Aug 10 '18 at 19:49 -
-
1@kirma Yes. What I meant"
RegionBoundary@Cuboid[]returns a list of quadrilaterals wrapped inPolygon. Mathematica would have to run planarity checks when callingRandomPoint. While this would be possible, I can understand that it would not be worthwhile to implement that, in particular since there is already an implementation for simplicialMeshRegions. And an automatica conversion would have also its pitfalls. But I agree that I was also puzzled first when I realized thatRandomPoint@RegionBoundary@Cuboid[]does not evaluate. – Henrik Schumacher Aug 10 '18 at 20:04 -
2I believe the need for DiscretizeRegion is a bug, and should be reported. It used to work in 11.1. Also, it works in higher dimensions, e.g.,
RandomPoint @ RegionBoundary @ Cuboid[{0,0,0,0}, {1,1,1,1}]. – Carl Woll Aug 10 '18 at 21:53 -
1
3
ReplacePart[Table[RandomReal[], 3],
RandomChoice[Range[3]] -> RandomInteger[{0, 1}]]
If you want points on the surface of a $d$-dimensional unit hypercube:
d = 5;
ReplacePart[Table[RandomReal[], d],
RandomChoice[Range[d]] -> RandomInteger[{0, 1}]]
David G. Stork
- 41,180
- 3
- 34
- 96
-
-
I don't know about faster. Compared to
RandomPoint[R, 1000], how long does your method take to generate 1000 random points? – Carl Woll Aug 10 '18 at 20:36 -
@David G. Stork. That depends. If you count in the timing for discretizing the region: Yes, this takes a lot if time. But you have to do that only once. – Henrik Schumacher Aug 10 '18 at 20:41
-
You used
Table[RandomPoint[R], 10^6]instead ofRandomPoint[R, 10^6]. The latter is about 3 orders of magnitude faster. – Carl Woll Aug 10 '18 at 21:46 -
-
I believe that at higher dimensions, vectorizing your approach would be faster than using RandomPoint. – Carl Woll Aug 10 '18 at 22:05
-
2
Here is how to build it from scratch in case someone finds that interesting:
coords = {{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 1}, {0, 1, 1}, {1, 1, 1}, {1, 0, 1}};
pts = {{4, 3, 2, 1}, {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 4, 8, 7}, {4, 1, 5, 8}, {5, 6, 7, 8}};
polygons = Flatten@Normal@GraphicsComplex[coords, Polygon[pts]];
reg = RegionUnion[DiscretizeRegion /@ polygons];
RandomPoint[reg]
I grabbed the coordinates from the documentation for Hexahedron.
C. E.
- 70,533
- 6
- 140
- 264
ListPointPlot3D[RandomSample /@ Transpose[RandomReal[{0, 1}, {2, 1000}]~Join~{RandomInteger[{0, 1}, 1000]}], BoxRatios -> {1, 1, 1}]– AccidentalFourierTransform Aug 10 '18 at 23:47