[R] How to generate the random numbers uniformly distributed on the unit disc?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Oct 9 12:10:33 CEST 2006


"S.Q. WEN" <randsnews at gmail.com> writes:

> Hi,
> I want to get  random number which is uniformly distributed  on the unit
> disc.
> How can I do that with R?

Most easily, although perhaps not most efficiently, by using spherical
coordinates:

 N <- 10000
 r <- sqrt(runif(N))
 theta <- runif(N,0,2*pi)
 x <- r*cos(theta)
 y <- r*sin(theta)
 plot(x,y,pch=".")




-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list