# Generate random point inside a polyhedron

I would like to know if there are known algorithms for generating a random point(say following a probability distribution P) inside an arbitrary polyhedron(possibly non-convex and may contain holes)?

I find approaches for 2D case like the one described here but none in the case of polyhedrons(3D). Whether extending this approach for 2D is the only way, in which case that random point will still be forced to be on the surface of a triangle of a tetrahedron in the 3D triangulation of the given polyhedron?

If you polytope is convex, this answer should work. Otherwise you can always do rejection sampling: Sample a point from the bounding box, check whether it's inside the polygon. That gets really slow for high dimensions and "unboxy" polytopes though, because the bounding box potentially has a huge volume compared to the region you're interested in.

• I am using polyhedrons to represent machine components which are three dimensional and non-convex. BTW, what do you mean by "unboxy" polytopes? – Pranav Jul 14 '16 at 8:31
• If the polytope looks like a hypercube, sampling by using the bounding hypercube works ok. If it is very different from a cube, the runtime explodes. – adrianN Jul 14 '16 at 8:43
• Will this approach for convex polytope work if the polytope has holes...I mean will that random point always lie inside polytope but not in hole? – Pranav Jul 14 '16 at 11:58
• If it has holes, it's not convex. You will have to read the papers to see whether the methods can be adapted and perhaps ask a new question. – adrianN Jul 14 '16 at 12:13
• I have edited this question accordingly. – Pranav Jul 14 '16 at 12:29

To implement adrianN's rejection sampling method, you will need a function to determine if a point is inside your polyhedron. This not-completely-trivial problem is discussed in my book Computational Geometry in C, and there is code available. • Will it also work for polyhedrons with holes?...Actually, in my problem, I get this polyhedron after symmetric difference between 2 input polyhedrons, which often creates holes. – Pranav Jul 14 '16 at 13:35
• @Pranav: Yes, because it just counts ray crossings. – Joseph O'Rourke Jul 14 '16 at 13:43