Re: [R] [OOPS] Re: Drawing sample from a circle

From: Ron Michael <ron_michael70_at_yahoo.com>
Date: Fri, 18 Jun 2010 11:18:41 -0700 (PDT)


Thank you so much Teds for pviding this function. Would you please explain the theory behind that?  
Thanks,

From: Ted.Harding_at_manchester.ac.uk <Ted.Harding_at_manchester.ac.uk> Subject: [OOPS] Re: [R] Drawing sample from a circle To: r-help_at_stat.math.ethz.ch
Cc: "Ron Michael" <ron_michael70_at_yahoo.com> Date: Friday, 18 June, 2010, 4:48 PM

OOPS: AN error on the code below! See in-line. Ted.

On 18-Jun-10 09:33:04, Ted Harding wrote:
> On 18-Jun-10 08:04:36, Ron Michael wrote:

>> Hi, I would like to draw 10 uniformly distributed sample points from a
>> circle with redius one and centered at (0,0). Is there any R function
>> to do that?
>> _
>> Thanks,

>
> You can quite easily write one.
>
> [A]
> Sampling uniformly on the circumference of the circle:
>
>   csamp <- function(n,rad=1,centre=c(0,0)){
>     x0 <- centre[1] ; y0 <- centre[2]
>     u <- 2*pi*runif(n)
>     rad*cbind(x=cos(u)+x0, y=sin(u)+y0)
>   }

>

># Returns an nx2 matrix whose two columns are the x and y coordinates CORRECTION:
  csamp <- function(n,rad=1,centre=c(0,0)){
    x0 <- centre[1] ; y0 <- centre[2]
    u <- 2*pi*runif(n)
    cbind(x=rad*cos(u)+x0, y=rad*sin(u)+y0)
  }

# Returns an nx2 matrix whose two columns are the x and y coordinates

> [B]
> Sampling uniformaly within the circle
>

>   Csamp <- function(n,rad=1,centre=c(0,0)){
>     x0 <- centre[1] ; y0 <- centre[2]
>     u <- 2*pi*runif(n)
>     r <- sqrt(runif(n))
>     rad*cbind(x=r*cos(u)+x0, y=r*sin(u)+y0)

> }
>

># Returns an nx2 matrix whose two columns are the x and y coordinates CORRECTION:
  Csamp <- function(n,rad=1,centre=c(0,0)){
    x0 <- centre[1] ; y0 <- centre[2]
    u <- 2*pi*runif(n)
    r <- sqrt(runif(n))
    cbind(x=rad*r*cos(u)+x0, y=rad*r*sin(u)+y0)
}

# Returns an nx2 matrix whose two columns are the x and y coordinates

> [C]
> Examples:
>

>   plot(csamp(100),asp=1)

>
>   plot(Csamp(1000),asp=1)

>
> Ted.
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 18-Jun-10                                       Time: 10:33:00
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> 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: 18-Jun-10                                       Time: 10:48:14
------------------------------ XFMail ------------------------------



	[[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. Received on Fri 18 Jun 2010 - 20:44:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Jun 2010 - 20:50:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive