What is best way or an algorithm for generating a random 3d point [x,y,z] inside the volume of the circular cylinder if radius r and height h of the cylinder are given?
-
These answers have been assuming that the cylinder is aligned with the z-axis. Are these answers sufficient, or do you need a way to calculate something similar cylinders which are say, along a non-standard axis? – Zéychin Feb 09 '12 at 00:42
-
The answers below also assume you want the points uniformly distributed within the volume of the cylinder. You should clearly indicate otherwise if that's not what you want. – andand Feb 09 '12 at 06:41
-
I like to have an approach if cylinder is along a non - standard axis – user1198477 Feb 09 '12 at 16:50
-
2@user1198477: The easiest way to accomplish what you're looking to do is to create the points in a canonical cylinder (e.g. height and radius both 1 and centered on the origin, and aligned along one of the axes) and then use affine transformations to move the points within this canonical cylinder to be contained within your desired cylinder. Creating them directly in your cylinder is going to be much more complicated than taking a two step approach. – andand Feb 09 '12 at 19:14
4 Answers
How about -- in Python pseudocode, letting R be the radius and H be the height:
s = random.uniform(0, 1)
theta = random.uniform(0, 2*pi)
z = random.uniform(0, H)
r = sqrt(s)*R
x = r * cos(theta)
y = r * sin(theta)
z = z # .. for symmetry :-)
The problem with simply taking x = r * cos(angle)
and y = r * sin(angle)
is that then when r is small, i.e. at the centre of the circle, a tiny change in r doesn't change the x and y positions very much. IOW, it leads to a nonuniform distribution in Cartesian coordinates, and the points get concentrated toward the centre of the circle. Taking the square root corrects this, at least if I've done my arithmetic correctly.
[Ah, it looks like the sqrt was right.]
(Note that I assumed without thinking about it that the cylinder is aligned with the z-axis and the cylinder centre is located at (0,0,H/2). It'd be less arbitrary to set (0,0,0) at the cylinder centre, in which case z should be chosen to be between -H/2 and H/2, not 0,H.)

- 342,061
- 65
- 592
- 494
-
I like to have an approach if cylinder is along a non - standard axis – user1198477 Feb 09 '12 at 16:51
Generate a random point inside the rectangular solid circumscribing the cylinder; if it's inside the cylinder (probability pi/4), keep it, otherwise discard it and try again.

- 96,650
- 16
- 149
- 150
-
1This has the **possibility** of taking a long time to terminate. Not worth it to downvote you, since you are not wrong, but this is a caveat worth mentioning. – Zéychin Feb 09 '12 at 00:40
Generate a random angle (optionally less than 2π), a random r
less than the radius, and a random z
less than the height.
x = r * cos(angle)
y = r * sin(angle)

- 868,454
- 176
- 1,908
- 1,964
-
1You should look at the link in DSM's post. Though the OP doesn't say the points should be uniformly distributed within the volume of the cylinder, it seems to be implied. Your solution will cluster points along the z axis more densely. To uniformly distribute, change `r` to `sqrt(r)`. – andand Feb 09 '12 at 06:38
-
@andand: Yes. I already upvoted him, but I didn't want to steal his answer. – SLaks Feb 09 '12 at 18:15
The z axis is easy: -0.5 * h <= z <= 0.5 * h
The x and y are equal to a circle will be: x^2 + y^2 <= r^2
Buth math is long ago for me :-)

- 15,025
- 28
- 93
- 119
-
-
I think it depends if you assume the cylinder is centered in z direction, my 'cylinder' center is at (0, 0, 0). – Michel Keijzers Feb 09 '12 at 01:03