samedi 27 février 2021

Generating solid spheres in python

I'd like to generate random uniform samples from n-dimensional solid spheres.

My current method is this

def sample_sphere(d, npoints):
    points = np.zeros((npoints, d))
    for i in range(npoints):
        r = np.random.rand() #random radius
        v = np.random.uniform(low= -1, high=1, size = d) #random direction
        v = v/np.linalg.norm(v)
        points[i] = r*v
    return points

But unsurpringly, this method gives a higher concentration of points near 0, (see image) since the sampling density is not proportional to the volume. How can I sample uniformly?




Aucun commentaire:

Enregistrer un commentaire