简体   繁体   English

用Python随机设定初始位置

[英]Making random initial position by Python

I am now working to make rotational trajectories. 我现在正在努力制作旋转轨迹。 In the beginning I need to define the initial position of an rotating object. 首先,我需要定义旋转对象的初始位置。 How to make 1000 random initial position in three dimensions of this kind of object by Python or NumPy? 如何通过Python或NumPy在此类对象的三维中使1000个初始位置随机化? I think a python function can solve the problem. 我认为python函数可以解决问题。

If I understand the question, you need to pick up point uniformly distributed on a sphere. 如果我理解这个问题,则需要拾取均匀分布在球体上的点。 See here for details. 有关详细信息,请参见此处

import math
import numpy as np

cos_theta = 2.0 * np.random.random(100) - 1.0
phi       = 2.0 * math.pi * np.random.random(100)

sin_theta = np.sqrt( (1.0 - cos_theta)*(1.0 + cos_theta) )

x = sin_theta * np.cos(phi)
y = sin_theta * np.sin(phi)
z = cos_theta

print(x, y, z)
print("---------------------")
print(np.square(x) + np.square(y) + np.square(z))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM