I have to randomly generate 1000 points over a unit disk such that are uniformly distributed on this disk. Now, for that, I select a radius r and angular orientation \alpha such that the radius r is a uniformly distributed variate from r \in [0,1] while \alpha is a uniformly distributed variate from \alpha \in [0, 2\pi] using the following code

`r <- runif(1000, min=0, max=1) alpha <- runif(1000, min=0, max=2*pi) x <- r*cos(alpha) y <- r*sin(alpha) plot(x,y, pch=19, col=rgb(0,0,0,0.05), asp=1)`

Then I look at my sample space and it looks like this:

This obviously doesn’t look like a sample with uniform distribution over the disk. Hence, I guessed that the problem might be occurring as a result of a lack of independence between the variables r and \alpha in contingency to how they’ve been linked computationally.

To take care of that I wrote a new code.

`rm(list=ls()) r <- runif(32, min=0, max=1) df_res <- data.frame(matrix(c(-Inf, Inf), byrow = T, nrow = 1)) for (i in 1:32) { for (j in 1:32) { alpha <- runif(32, min=0, max=2*pi) r <- runif(32, min=0, max=1) df <- data.frame(matrix(c(r[i],alpha[j]), byrow = T, nrow = 1)) df_res <- rbind(df_res,df) } } df_res <- subset(df_res, df_res$X1 != -Inf) x<- df_res$X1 *cos(df_res$X2) y <- df_res$X1 *sin(df_res$X2) plot(x,y, pch=19, col=rgb(0,0,0,0.05), asp=1)`

And, yet again the sample looks non-uniformly distributed over the disk

I’m starting to suspect that there is a deeper mathematical problem going on in the vicinity. Could someone help me write code that would create a sample space uniformly distributed over the disk, or explain the mathematical fallacy if any in my reasoning?

**Answer**

The problem is due to the fact that the radius is not uniformly distributed. Namely, if (X,Y) is uniformly distributed over

\left\{ (x,y);\ x^2+y^2\le 1\right\}

then the (polar coordinates) change of variables

R=(X^2+Y^2)^{1/2}\qquad A=\text{sign}(Y)\arccos(X/R)

has the density

\frac{1}{\pi} \mathbb{I}_{(0,1)}(r)\left|\frac{\text{d}(X,Y)}{\text{d}(R,A)}(r,\alpha)\right|\mathbb{I}_{(0,2\pi)}(\alpha)

Using x = r \cos \alpha and y = r \sin \alpha leads to

\left|\frac{\text{d}(X,Y)}{\text{d}(R,A)}(r,\alpha)\right|=r(\sin^2\alpha+\cos^2\alpha)=r

Therefore, the angle A is distributed uniformly over (0,2\pi) but the radius R has density f(r)=2r\mathbb{I}_{(0,1)}(r) and cdf F(r)=r^2 over (0,1). As one can check by running

```
r <- sqrt(runif(1000, min=0, max=1) )
alpha <- runif(1000, min=0, max=2*pi)
x <- r*cos(alpha)
y <- r*sin(alpha)
plot(x,y, pch=19, col=rgb(0,0,0,0.05), asp=1)
```

where the radius is simulated by the inverse cdf representation, which makes it the square root of a Uniform variate, the random repartition of the 10³ simulated points is compatible with a uniform:

**Attribution***Source : Link , Question Author : ubuntu_noob , Answer Author : Xi’an*