Skip to content

Instantly share code, notes, and snippets.

@hypercompetent
Created April 21, 2021 18:17
Show Gist options
  • Save hypercompetent/df1327c1a4bfab0c782101170a19e4d4 to your computer and use it in GitHub Desktop.
Save hypercompetent/df1327c1a4bfab0c782101170a19e4d4 to your computer and use it in GitHub Desktop.
Circular and Spherical random uniform distribution functions
rcau <- function() {
angle <- 2 * pi * runif(1)
radius <- 1 * (runif(1) ^ (1/2))
x <- radius * cos(angle)
y <- radius * sin(angle)
c(x, y)
}
coord_mat <- matrix(0, nrow = 2000, ncol = 2)
for(i in 1:nrow(coord_mat)) {
coord_mat[i,] <- rcau()
}
plot(coord_mat)
library(rgl)
rsau <- function() {
theta <- acos(runif(1) * 2 - 1)
phi <- 2 * pi * runif(1)
radius <- 1 * (runif(1) ^ (1/3))
x <- radius * cos(phi) * sin(theta)
y <- radius * sin(phi) * sin(theta)
z <- radius * cos(theta)
c(x, y,z)
}
coord_mat <- matrix(0, nrow = 2000, ncol = 3)
for(i in 1:nrow(coord_mat)) {
coord_mat[i,] <- rsau()
}
points3d(coord_mat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment