While this looks like a basic problem, it is actually somewhat more complicated than expected. One might think of generating uniform deviates for the longitude, as well as uniform deviates for the latitude. This will over-sample near the poles and under-sample near the equator: the resulting point distribution won't be uniform on the surface of the sphere. This has serious implications if one wants to measure (say) the average temperature on Earth using (say) 1,000 random locations. If these locations are not random, it will introduce a bias.
How do we fix this problem?
One easy way to produce random points uniformly distributed on a sphere centered at the origin, of radius 1, is to
The classic solution is different but produces the same uniform distribution on the sphere:
Note that step #2 in the second algorithm is identical to step #3 in the first algorithm.
Related article
Tags:
does using the following expression give you bias ?
z = sqrt(r^2 - x^2 - y^2), where x, y are random numbers between [-1, 1].
I just check it with excel with 2 dimension and it seems unioform deviates.
Could you not also, instead of using a Cartesian coordinate system, use a spherical coordinate system? That way you only have to generate two random variables between 0 and p (θ and φ) since r is fixed.
yeah I just tessted with excel. And the pinpoints were not that bad.
I wonder how is it possible to distribute densely near to the poles ??
Alan Ross Groskreutz said:
Could you not also, instead of using a Cartesian coordinate system, use a spherical coordinate system? That way you only have to generate two random variables between 0 and p (θ and φ) since r is fixed.
There is in fact a simple and fast way to do it in spherical coordinates. The idea is that the longitude can be drawn from a uniform distribution but the latitude must be drawn from a uniform distribution of the projected angle and then transformed back to an angle, to guarantee an uniform density at all latitudes. For example:
import numpy as np
N = 1000
alpha = np.random.rand(N)*2.0*np.pi
phi = np.arcsin(np.random.rand(N)*2.0-1.0)
this code written in c does it, too: https://github.com/jcoupon/venice
© 2019 Data Science Central ® Powered by
Badges | Report an Issue | Privacy Policy | Terms of Service
Most Popular Content on DSC
To not miss this type of content in the future, subscribe to our newsletter.
Other popular resources
Archives: 2008-2014 | 2015-2016 | 2017-2019 | Book 1 | Book 2 | More
Most popular articles