-1

Possible Duplicate:
Generating random point in a cylinder

How to generate a random point inside a circular cylinder if radius r and height h are given?

particularly inside the cylinder not on the cylinder

Community
  • 1
  • 1
user1198477
  • 53
  • 1
  • 6
  • Just to be clear... "Inside" meaning a point anywhere in the volume of the cylinder, or a random point on the surface? – Adam S Feb 13 '12 at 19:32
  • @ Adam S :anywhere in the volume of the cylinder – user1198477 Feb 13 '12 at 19:52
  • @user1198477 this question is very soon going to get closed as an exact duplicate of the one you asked 6 days ago. Is there anything *different* this time? If so, you should edit your question to say what. – AakashM Feb 14 '12 at 09:04

1 Answers1

3

Something like:

angle = Random(0, 2*Pi)
r1 = r * sqrt( Random(0,1) )
X = r1*sin(angle)
Y = r1*cos(angle)
Z = Random(0,h)
Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • @PeterdeRivaz, why do you have sqrt? Wouldn't you just want a random radius between 0 and r? – TJD Feb 13 '12 at 19:35
  • [Edited to clarify:] You need to take r * the sqrt of the random number, not sqrt(r*random()). Say r were 5 -- then sqrt(5*random()) would never be greater than sqrt(5). – DSM Feb 13 '12 at 19:35
  • @DSM : do you mean r1= r* sqrt(Random(0,1)) in the above mentioned code – user1198477 Feb 13 '12 at 20:01
  • @DSM Agreed, you need r1=r*sqrt( Random(0,1) ) in this answer. TJD you need sqrt to avoid getting a bigger concentration of points near the centre – Peter de Rivaz Feb 13 '12 at 21:43
  • Here's a nice blurb explaining the significance of needing square root: http://www.anderswallin.net/2009/05/uniform-random-points-in-a-circle-using-polar-coordinates/ – Brent Worden Feb 14 '12 at 00:06