Skip to content
Prev 206924 / 398503 Next

question on runif

Alfredo's solution will provide n (=200 in his case) points
uniformly distributed along the *circumference* of the circle.
Wolfgang_Amadeus(!), you wanted them "*in* a circle".

If what you mean is "uniformly distributed over the area within
the circle", then you also need to generate the radii at random.

If P, with polar coordinates (r,t), is uniformly distributed
over the internal area of a circle with radius R and centre (0,0),
then the angle t is uniformly distributed over (0,2*pi) as in
Alfredo's solution, and the density of r is 2*(r/R^2) and the
CDF of r is (r/R)^2.

If U is uniformly distributed on (0,1), then Y = sqrt(U) has
the CDF y^2, since
  P(Y < y) = P(sqrt(U) < y) = P(U < y^2) = y^2.

Hence:

  r0 <- 0.1
  X0 <- 0.4
  Y0 <- 0.8
  n  <- 200
  r  <- r0*sqrt(runif(n))
  t  <- 2*pi*runif(n)
  X  <- X0 + r*cos(t)
  Y  <- Y0 + r*sin(t)
  plot(X,Y)

Ted.
On 20-Jan-10 11:31:49, Alfredo wrote:
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Jan-10                                       Time: 12:16:05
------------------------------ XFMail ------------------------------