Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Random coordinate

  1. Mar 15, 2010 #1
    How do i randomize a position on a sphere? Using a random number between 0 and 360 for longitude and a number between -90 to 90 for latitude would make it more probable to get closer to the poles right?
  2. jcsd
  3. Mar 15, 2010 #2
    picking 3 random numbers between 0 and 1 for the x,y and z coordinates sounds like a better bet...
  4. Mar 15, 2010 #3


    Staff: Mentor

    Almost all of the time, this would give you a point inside a sphere of radius 1, which isn't what the OP asked for. For a point to be on the surface of the sphere, the squares of the coordinates would have to add to the square of the radius.
  5. Mar 15, 2010 #4
    Apologies...didn't read the question properly. How about just picking the x and y coordinates as I suggested, and then randomly choosing whether the z-coordinate is positive or negative?
  6. Mar 15, 2010 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Drawing sine(latitude) from U[-1,1] and longitude from U[0,360º) does the trick.

    A good random number package will provide separate interfaces for drawing from U[0,1), U[0,1], U(0,1), and U(0,1]. A not so good one might only provide just one of these four. There are some very small drawbacks to using not quite the right interface. For example, drawing longitude from U[0,360º] gives a small chance of doubling up on 0 versus 360º. Drawing sine(latitude) from U[-1,1) means you ruled out the North Pole.
  7. Mar 15, 2010 #6
    Uhm, i have absolutely no idea what this means. I only know very basic math. And in case i was unclear in my first post, i want an even distribution, not centered to the poles.
  8. Mar 15, 2010 #7
    Just thought of a blunt way of doing it. I peel the sphere in to say 24 sections and lay them out flat, then draw a rectangle around all of it and randomizes a number inside the rectangle, a simpel X and Y value, and those points that miss the peels simply doesn't count. It's not pretty but i it'll work. Unless of course someone has a easier method.
  9. Mar 15, 2010 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    What I gave will spread the points out uniformly over the globe.

    I'll keep it simple -- after saying what U(a,b) and the related terms mean. Colloquially, U(a,b) means "a random number drawn from between a and b". The "U" means uniform; there are many other distributions besides uniform. The difference between square brackets and parentheses just means whether the endpoints are included or excluded from the set from which the numbers are drawn.

    So, what does this mean? Draw a random number from between -1 and 1. Take the inverse sine of this number and express the result in degrees. This is your latitude. Draw a random number from between 0 and 360º. This is your longitude. If you draw a whole bunch of latitude,longitude pairs this way and plot their locations you will see that this gives a nice uniform spread over the globe. As you have already discovered, drawing latitude directly from between -90º and 90º does not give a nice uniform spread.
  10. Mar 15, 2010 #9
    It works fine. Got some help from my sister though. Here's what i (her) came up with in Excel for the latitude.


    Thanks D H!
  11. Mar 15, 2010 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You're welcome.

    BTW, =DEGREES(ASIN(RAND()*2-1)) would work quite nicely as well.

    My version of Excel doesn't even have a RANDBETWEEN function.
  12. Mar 16, 2010 #11


    User Avatar
    Science Advisor

    I know this is probably not much help to the OP, but it’s interesting to point out that DH’s method is mathematically equivalent to choosing latitude from a random distribution with a cosine probability density function,

    [tex]f(\phi) = 1/2 \cos(\phi) \, : \, -\pi/2 \leq \phi \leq \pi/2[/tex]

    Since an element of surface on the sphere is (taking phi as latitude and theta as longitude) given by :

    [tex]dS/r^2 = \cos(\phi) \, d\phi \, d\theta[/tex]

    and [itex]\cos(\phi) d\phi[/itex] is (for the given pdf) proportional to the probability that latitude is between [itex]\phi[/itex] and [itex]\phi + d\phi[/itex] then it follows that the probability of "hitting" a given element of surface on the sphere is proportional only to the SA of the element, no matter where that element of SA is located.
    Last edited: Mar 16, 2010
  13. Mar 16, 2010 #12
    I'd probably ask for three random numbers in the range [-1, 1] and calculate the two angles of that vector, effectively projecting it onto a sphere. if you happen to get (0, 0, 0) then try again.

    No polar bias doing it this way, though you need 3 random numbers per point rather than the minimum 2.
  14. Mar 16, 2010 #13

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    There's bias all over the place doing it this way!

    In the vicinity of the projections onto the sphere of areas in the vicinity of
    • The centers of the six faces of the cube: Too few points.
    • The twelve edges of the cube: Too many points.
    • The eight corners of the cube: Way too many points.
  15. Mar 16, 2010 #14
    Ok I see, I had it very confused. Disregard my first post!
  16. Mar 16, 2010 #15

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Think of it in terms of the volume of the cube that is inside a small cone with vertex at the center of the cube. The volume varies when the cone is aimed at a face versus a corner.

    Your approach can be made to work. Reject x,y,z triples whose distance to the origin exceeds one. In other words, try again (and again and again if needed) until you get a triple that is inside the unit sphere. That way you are generating points inside the unit sphere rather than inside a cube.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook