You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用拒绝采样法生成均匀球分布出现椭球畸变的技术求助

问题:生成单位球内均匀分布点出现椭球形异常

我尝试用Python的均匀随机采样生成单位球(满足x²+y²+z²≤1)内的均匀分布点,但生成的分布呈现椭球形:xy平面投影是标准圆形,xz、yz平面却显示为椭圆形态。附上我的实现代码和结果图,无法定位问题所在,寻求帮助。

import numpy as np
import numpy.random as rand
import matplotlib.pyplot as plt

N = 10000

def sample_cube(number_of_trials):
    """
    该函数接收一个整数,返回立方体内的均匀采样点。
    返回对象是一个列表,包含三个子列表,分别对应空间中点的x、y、z坐标。
    """
    cube = rand.uniform(low = -1, high = 1, size = (number_of_trials, 3))
    x_cube = cube[:,0]
    y_cube = cube[:,1]
    z_cube = cube[:,2]
    return [x_cube, y_cube, z_cube]

def sample_sphere(cube):
    """
    该函数接收格式为[x_cube, y_cube, z_cube]的列表,从中筛选出单位球内的点。
    """
    in_sphere = np.where(np.sqrt(cube[0]**2 + cube[1]**2 + cube[2]**2) <= 1)
    return in_sphere

"""
主代码
"""
cube = sample_cube(N)
sphere = sample_sphere(cube)
print(sphere[0])
print(cube)

print(cube[0][sphere[0]])
x_in_sphere = cube[0][sphere[0]]
y_in_sphere = cube[1][sphere[0]]
z_in_sphere = cube[2][sphere[0]]
fig = plt.figure()
ax = plt.axes(projection = "3d")
ax.scatter(x_in_sphere, y_in_sphere, z_in_sphere, s = 1, color = "black")
plt.show()
plt.clf

结果图

  • 生成的3D分布:生成的3D分布
  • xz平面侧视图:xz平面侧视图
  • xy平面俯视图:xy平面俯视图

内容的提问来源于Stack Exchange,提问作者Luis Miguel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:07:43