title: how to generate new points as offset with gaussian distribution for some points in spherical coordinates in python
body: <p>I am working with some points in spherical coordinates. I need to generate new points as the error points for them and a kind of offset for the old points.
  The new point should be in a specific distance from the old one which distributing by gaussian distribution. The angle of new point compared to old one is not important.I am trying to generate new points for r direction. no matter what are phi and theta (Spherical coordinates)</p>
  
  <p>To generate the new point distributing by gaussian function, I tried the numpy.rand.normal(mean,std,..). But It is generating 1D random points over mean value and this mean value is a real number. In my case I need an approach to specify the position of the old point and I have one given standard deviation for this distance from the original points.</p>
  
  <p>Honesty, I dont have a copy of my code. It is on the university's server. But let's assume I have an array of size 100*3 including the spherical (or cartesian) coordinates of some points on a surface of a cylinder. In spherical case, the first column presents the radius value, the second column is theta and third one shows the phi for the points. now I want to generate random points from them using gaussian distribution. there is a given standard deviation for the gaussian distribution. The only important thing is that the new points generated by gaussian distribution are limited in r value. No matter the position of points in term of theta and phi.
  When I tried numpy.rand.normal(mean,std,..), this generate some random points over the mean value. It does not help me. I want new points over my old ones with the given STD.  </p>
  
  <p>any idea would be appreciated.</p>
  
  <p>This is a code, similar to mine written By Ophion <a href="https://stackoverflow.com/questions/22285994/how-to-generate-regular-points-on-cylindrical-surface">How to generate regular points on cylindrical surface</a></p>
  
  <p>def make_cylinder(radius, length, nlength, alpha, nalpha, center, orientation):</p>
  
  <pre><code>#Create the length array
  I = np.linspace(0, length, nlength)
  
  #Create alpha array avoid duplication of endpoints
  #Conditional should be changed to meet your requirements
  if int(alpha) == 360:
      A = np.linspace(0, alpha, num=nalpha, endpoint=False)/180*np.pi
  else:
      A = np.linspace(0, alpha, num=nalpha)/180*np.pi
  
  #Calculate X and Y
  X = radius * np.cos(A)
  Y = radius * np.sin(A)
  
  #Tile/repeat indices so all unique pairs are present
  pz = np.tile(I, nalpha)
  px = np.repeat(X, nlength)
  py = np.repeat(Y, nlength)
  
  points = np.vstack(( pz, px, py )).T
  
  #Shift to center
  shift = np.array(center) - np.mean(points, axis=0)
  points += shift
  
  #Orient tube to new vector
  
  #Grabbed from an old unutbu answer
  def rotation_matrix(axis,theta):
      a = np.cos(theta/2)
      b,c,d = -axis*np.sin(theta/2)
      return np.array([[a*a+b*b-c*c-d*d, 2*(b*c-a*d), 2*(b*d+a*c)],
                       [2*(b*c+a*d), a*a+c*c-b*b-d*d, 2*(c*d-a*b)],
                       [2*(b*d-a*c), 2*(c*d+a*b), a*a+d*d-b*b-c*c]])
  
  ovec = orientation / np.linalg.norm(orientation)
  cylvec = np.array([1,0,0])
  
  if np.allclose(cylvec, ovec):
      return points
  
  #Get orthogonal axis and rotation
  oaxis = np.cross(ovec, cylvec)
  rot = np.arccos(np.dot(ovec, cylvec))
  
  R = rotation_matrix(oaxis, rot)
  return points.dot(R)
  </code></pre>
  
  <h1>now calling the function:</h1>
  
  <pre><code>points = make_cylinder(3, 5, 5, 360, 10, [0,2,0], [1,0,0])
  </code></pre>
  
  <p>sigma = 0.5 # given STD
  ossfet_points = numpy.random.normal(np.mean(point[:,0]), sigma, size=(n,3))</p>