Point me in a random direction (computer algorithim needed)

I’m looking for a link or description of an algorithm that takes some random numbers between 0 and 1 and returns a 3-d vector that points in a random direction (preferably in Cartesian co-ordinates and of magnitude one, but I can do the conversion/normalization if it returns a vector of a different form). I haven’t been able to google up anything and can’t make it to the library till monday, but I figure this must be a fairly common problem so perhaps a doper will know the answer, or at least point me in the correct direction (heh).

Before I get a bunch of folks suggesting the obvious solutions, I’m fairly certain that neither just assigning a random number in [-1,1] to each of the three cartesian cordinates or a random angle to theta and phi in spherical co-ordinates gives a vector with a truly random direction.

See question 6.08 on this page.

Ah, that looks like it’ll work, thanks.

Interestingly, looks like both of your rejected proposals, while correctly rejected, are very close to usable methods. You can assign a quantity uniformly drawn from [-1, 1] to each of the three Cartesian coordinates as long as you discard and retry if the result has magnitude greater than 1 or equal to 0 (normalizing the eventual result if you feel the need). Or you can assign an angle uniformly drawn from [0, 2pi) to theta and then pick a phi, as long, as you don’t pick phi by drawing uniformly from all angles, but, rather, do it by choosing a height uniformly drawn from the possible heights.

It’s too bad that the second method is essentially tied to the 3d case, though (where the assumption of uniformly distributed height works out). It’d be nice to have a simple method for arbitrary dimension which didn’t involve any chance of repeated retries.

I’m not sure I follow this. If you choose phi by drawing uniformly from all possible heights, won’t you skew the results away from angles near vertical?

Yes and no, in various senses. In terms of points upon the Earth, the collection of points within five miles of the North pole is exactly as likely to be hit as, say, the collection of points within five miles of Detroit, or the collection of points within five miles of some point on the equator. In this sense, there is no special anti-vertical skew, in that the North pole is treated symmetrically with any other point. However, in terms of the particular measure of verticality of latitude, yes, latitude will be skewed away from +90. Latitude will not be uniformly distributed. But this is actually the right thing.

The right notion of a uniform distribution of directions (i.e., of a random direction) is such that the probability of the direction falling within a certain region on some fixed sphere is proportional to the area of that region. [This is the unique distribution with the desirable property of rotation invariance: if region R can be transformed into region S by a rotation, then R and S will have the same probability of being landed on; you get the North pole, Detroit, equator disc equivalences from before, for example.] However, you’ll note that the surface area of the portion of a sphere between, say, +90 and +80 degrees latitude is smaller than the surface area of the portion between +80 and +70 degrees. And other such things. So, yes, there is a “skewing” in a sense, in that the distribution of latitudes will not be uniform. But this is the right skewing, in that it’s the one which corresponds to a uniform distribution of directions. As explained before, it’s only necessarily a skewing away from vertical if you interpret that strictly in terms of latitude (which is a poor measure for these purposes, for precisely the reason that more of the surface area of the Earth is caught up in less extreme latitudes); if you look at it in other ways, it doesn’t seem like a skewing away from vertical at all.

To be pedantic about it, it’s not unique (one could, for example, devise an alternate such distribution in which the probability of getting a rational longitude was 100%); however, the probabilities of open regions are uniquely determined (and so the brunt of the above post remains the same; latitudes will still be distributed in the same non-uniform way).

Clarifying word inserted in bold. I should have just said Ecuador…

This isn’t true on the standard axiomatization of probability; I don’t know what I was thinking. (Well, I do know, but I don’t feel like explaining my mistake). The original claim to uniqueness was justified after all.

To pick a nit - Ecuador is bigger than Detroit, which is probably bigger than the North Pole.


And I’ll toss in another nit of my own, which is that the Earth isn’t a perfect sphere. But rather than pedantically patching up every possible piddling problematic point, I’ll just make the ultimate time-saving disclaimer: everything I said was essentially right, even where it happened to be technically false. Hell, apply this to everything I say from here on out. :slight_smile: