[R] How to generate the random numbers uniformly distributed on the unit disc?
Joerg van den Hoff
j.van_den_hoff at fz-rossendorf.de
Mon Oct 9 11:58:47 CEST 2006
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
More information about the R-help
mailing list