S.Q. WEN
2006-Oct-09 09:28 UTC
[R] How to generate the random numbers uniformly distributed on the unit disc?
Hi, I want to get random number which is uniformly distributed on the unit disc. How can I do that with R? Best wishes, WAN WAN [[alternative HTML version deleted]]
Joerg van den Hoff
2006-Oct-09 09:58 UTC
[R] How to generate the random numbers uniformly distributed on the unit disc?
S.Q. WEN wrote:> Hi, > I want to get random number which is uniformly distributed on the unit > disc. > How can I do that with R? > > > Best wishes, > WAN WAN > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.the following function should put "N" uniformly distributed points into a disc of radius "R" (and plot the points in the (phi,rho) and (xx, yy) coordinate systems). disc <- function (N = 10^4, R = 1) { phi <- runif(N) * 2 * pi rho <- R * sqrt(runif(N)) xx <- rho * cos(phi) yy <- rho * sin(phi) layout(1:2) plot(phi, rho, pch = ".") plot(xx, yy, pch = ".", asp = 1) layout(1) invisible(list(phi = phi, rho = rho)) } the trick is to transform a uniform distribution along `rho' in such a way that it gets "denser" towards the rim in such a way that the 'smearing out' of the points over the circumference is compensated. regards joerg
Peter Dalgaard
2006-Oct-09 10:10 UTC
[R] How to generate the random numbers uniformly distributed on the unit disc?
"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
Seemingly Similar Threads
- seg.fault from nlme::gnls() {was "[R-sig-ME] GNLS Crash"}
- R algorithm/package for creating spatial autocorrelation of uniformly distributed landscape values
- How to generate multivariate uniform distribution random numbers?
- how to generate uniformly distributed random integers
- Drawing sample from a circle