mardi 27 novembre 2018

Discard method to generate uniform random points in sphere

I'm trying to use the discard method to randomly generate coordinates within a sphere in python (generate points in a cube and 'discard' those outside the sphere) but ideally I just want to replace the 'discarded' points with a point that DOES lie in the sphere.

N = 10000

position = numpy.random.uniform(-100, 100, (N,3))
for i in range(N):
    if numpy.linalg.norm(position[i]) > 100:
        position[i] = numpy.random.uniform(-100, 100)

fig = pyplot.figure()
ax = Axes3D(fig)
ax.scatter(position[:,0], position[:,1], position[:,2])
pyplot.show()

This is what I've tried, and it seems to almost work, but I get this weird line through the sphere and I can't figure out how to get rid of itplot of code




Aucun commentaire:

Enregistrer un commentaire