def sample_sphere3d(radius=1., n_samples=1):
"""
Sample points from 3D sphere.
@param radius: radius of the sphere
@type radius: float
@param n_samples: number of samples to return
@type n_samples: int
@return: n_samples times random cartesian coordinates inside the sphere
@rtype: numpy array
"""
from numpy.random import random
from numpy import arccos, transpose, cos, sin, pi, power
r = radius * power(random(n_samples), 1 / 3.)
theta = arccos(2. * (random(n_samples) - 0.5))
phi = 2 * pi * random(n_samples)
x = cos(phi) * sin(theta) * r
y = sin(phi) * sin(theta) * r
z = cos(theta) * r
return transpose([x, y, z])
评论列表
文章目录