[R] question on runif

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Jan 20 13:16:08 CET 2010


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:
> Oops forgot to include the radius
> 
> ang<-runif(200,0,2*pi)
> x<-cos(ang)*.1 +0.4
> y<-sin(ang)*.1 +0.8
> 
> plot(x,y)
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On
> Behalf Of Wolfgang_Amadeus
> Sent: Wednesday, January 20, 2010 5:26 AM
> To: r-help
> Subject: [R] question on runif
> 
> Hello! 
> I have a question on uniform distribution. 
>  I want to plot n, say 20, points, uniformly distributed, in a circle,
> with
> radius=0.1 and center,say, (0.4, 0.8)
> I do not know how~ 
>  Thank you for your time.
>  Yours 
> Wolfgang Amadeus
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
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 ------------------------------



More information about the R-help mailing list