Skip to content

Instantly share code, notes, and snippets.

@jgs03177
Created October 28, 2024 06:45
Show Gist options
  • Save jgs03177/0194f2fb6cdc0f66bb0c2979b83a63d6 to your computer and use it in GitHub Desktop.
Save jgs03177/0194f2fb6cdc0f66bb0c2979b83a63d6 to your computer and use it in GitHub Desktop.
uniform sample from d-ball
def uniformsample_dball(npoint, nd=2):
# https://extremelearning.com.au/how-to-generate-uniformly-random-points-on-n-spheres-and-n-balls/
# # method 20
eps = 1e-12
u = np.random.normal(0,1, (npoint,nd))
norm = np.sum(u ** 2, axis=1) ** 0.5
r = np.random.random(npoint)**(1.0/nd)
x = (r / (norm+eps)).reshape((-1,1)) * u
return x
def uniformsample_dball2(npoint, nd=2):
# https://extremelearning.com.au/how-to-generate-uniformly-random-points-on-n-spheres-and-n-balls/
# # method 22
u = np.random.normal(0,1, (npoint,nd+2))
norm = np.sum(u ** 2, axis=1) ** 0.5
x = (u/norm.reshape((-1,1)))[:,:nd]
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment