TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Generating random points inside a sphere

252 pointsby kkaranthalmost 7 years ago

28 comments

woopwoopalmost 7 years ago
A note about the rejection method: in high dimensions, this becomes very expensive. This is because the volume of a ball inscribed in a unit cube goes to zero (very quickly) as the dimension goes to infinity, so you start rejecting points with high probability.
评论 #17689577 未加载
评论 #17692028 未加载
评论 #17690030 未加载
评论 #17696766 未加载
评论 #17689119 未加载
charmidesalmost 7 years ago
The graphics are nice, but this article really could have used some complexity analysis and some probability theory.<p>The author neither discusses the asymptomatic complexity of the algorithms, nor the run-time of his implementation (which is more pertinent here), nor gives any proofs of why some of the algorithms sample uniformly from the unit ball and why some of them don&#x27;t.<p>Also, it would have been really nice to generalize this problem to n dimensions. I assume that there is some small value of n where naive rejection sampling is worse in practice than one of the more sophisticated methods.
评论 #17689351 未加载
mwkaufmaalmost 7 years ago
&quot;about 48% of the points chosen are discarded. This may not be a huge problem if the number of points that need to be generated is small&quot;<p>... or if the number of points isn&#x27;t small -- it&#x27;s only 2x the cost of the simple linear case without a loss of uniformity or adding any costly trig functions. That&#x27;s still pretty good!
评论 #17689451 未加载
acidburnNSAalmost 7 years ago
Correct uniform sampling in things like this comes up a lot in Monte Carlo radiation transport problems and is covered very well in your basic first year graduate Monte Carlo class in a good nuclear engineering program. If you want to go deeper, check out the literature around that.
评论 #17688844 未加载
评论 #17689395 未加载
plgalmost 7 years ago
That (last method) is a lot of sines and cosines, cube root and an acos... I bet for 3D the rejection method is not that much slower, in wall clock time.<p>OK so I coded up the first one and the last one in C and compiled on my mac using whatever gcc links to. I found 1e7 points in the sphere:<p>first routine: 0.521s<p>last routine: 0.850s<p>[edit]: C code here: <a href="https:&#x2F;&#x2F;pastebin.com&#x2F;zH0BehSM" rel="nofollow">https:&#x2F;&#x2F;pastebin.com&#x2F;zH0BehSM</a>
评论 #17691204 未加载
评论 #17690964 未加载
评论 #17693873 未加载
评论 #17691996 未加载
yiyusalmost 7 years ago
This problem is equivalent to generating random orientations, which can be easily done generating random unit quaternions (see, for example, [1]). Each quaternion found will correspond to a point in the surface of a 4D sphere, which can be mapped to a point inside a 3D sphere.<p>[1] <a href="http:&#x2F;&#x2F;mathproofs.blogspot.com&#x2F;2005&#x2F;05&#x2F;uniformly-distributed-random-unit.html" rel="nofollow">http:&#x2F;&#x2F;mathproofs.blogspot.com&#x2F;2005&#x2F;05&#x2F;uniformly-distributed...</a>
评论 #17688869 未加载
wruzaalmost 7 years ago
Interestingly, it seems to be all about bending some distribution and then unbending it again. Orthogonal dimensions are independent, so choosing three random numbers generates uniform in-cube distribution. But once you bend it via polar coordinates or vectors, you “wrap” many points of polar&#x2F;vector abstract space to near the cartesian origin. And then you have to bend back, to make it uniform again. That raises interesting question on whether it relates to topology problems. Since generating N randoms is a fastest primitive operation, is there a quick and uniform translation from a cube to the sphere, such that all points are still uniform? Can we apply space-filling curve magic here (like Hilbert’s)? I feel like there is much more geometry power under that, but sadly I’m just a guy with no math bg.
评论 #17691734 未加载
arnarbialmost 7 years ago
The main issue with the second method, which isn&#x27;t discussed at all, is that it&#x27;s more likely to pick a direction towards the corners of the unit cube. You can even see that bias in the point cloud.<p>The problem with choosing d uniformly is also better explained by pointing out that shells of larger radius have larger surface, thus they&#x27;ll be underrepresented.
chiasalmost 7 years ago
Interestingly, the method in which you determine your &quot;random point&quot; can greatly affect the end result, so swapping out the methods (e.g. &quot;generate three coordinates and discard&quot; method for the &quot;generate a direction and a length&quot; method) may produce wildly different results.<p>An interesting illustration of this is Bertrand&#x27;s Paradox, which asks the question: &quot;if you draw a random chord inside a circle, what is the probability that its length is greater than sqrt(3)?&quot;. The correct answer is 1&#x2F;2. The correct answer is also 1&#x2F;3. Oh, and the correct answer is also 1&#x2F;4. It depends on three equally valid (and uniformly distributed) ways of defining your random chord.<p>See: <a href="http:&#x2F;&#x2F;web.mit.edu&#x2F;tee&#x2F;www&#x2F;bertrand&#x2F;" rel="nofollow">http:&#x2F;&#x2F;web.mit.edu&#x2F;tee&#x2F;www&#x2F;bertrand&#x2F;</a>
评论 #17692115 未加载
评论 #17692072 未加载
vzalivaalmost 7 years ago
When the author mentions &quot;normally distributed numbers&quot; it sounds like he is sampling a normal (Gaussian) distribution. In fact, he is sampling from a uniform distribution.
评论 #17689016 未加载
nalouriealmost 7 years ago
For people who like the simplicity of the rejection algorithm, it might be worth checking out Ziggurat sampling which is based on a similar idea, but shapes the sampling region to reduce rejections: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Ziggurat_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Ziggurat_algorithm</a>
评论 #17689392 未加载
tambourine_manalmost 7 years ago
Pleasantly surprised by the effect when scrolling over the sphere
评论 #17689124 未加载
评论 #17689306 未加载
DanAndersenalmost 7 years ago
Related: selecting points on the surface of a sphere <a href="https:&#x2F;&#x2F;www.jasondavies.com&#x2F;maps&#x2F;random-points&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.jasondavies.com&#x2F;maps&#x2F;random-points&#x2F;</a><p>Poisson disc sampling can be quite useful.
评论 #17693231 未加载
enugualmost 7 years ago
For the second example, sampling the radius uniformly leads to an error with a higher point density in the centre of sphere. The radius, r, has a weight proportional to r^2 or the surface area. So, one can take a uniform random sample, s between 0 and R^2, and take the square root of s. That will give higher probability density around larger radius. Then, the second picture should look like the first one.
评论 #17690636 未加载
评论 #17690689 未加载
anewhnaccount2almost 7 years ago
Similar problem which ends up not being quite as simple in the general case as you might think: sampling from a unit simplex: <a href="http:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~nasmith&#x2F;papers&#x2F;smith+tromble.tr04.pdf" rel="nofollow">http:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~nasmith&#x2F;papers&#x2F;smith+tromble.tr04.pdf</a>
nayukialmost 7 years ago
I think the section &quot;Using normally distributed random numbers&quot; deserves an additional explanation. The code may look simple, but like most of the other methods mentioned on the page, generating normally (Gaussian) distributed random numbers still requires the use of rejection sampling and sine&#x2F;cosine functions. See <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Box%E2%80%93Muller_transform" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Box%E2%80%93Muller_transform</a>
评论 #17690043 未加载
sgentlealmost 7 years ago
I thought this was kinda fun, and I was trying to figure out why you couldn&#x27;t just take a random x and use it to find a y that preserves x²+y²=r² (ie by setting y to a random number between 0 and ±√(r²-x²)). In the end I figured out that you need to generate x the same way, but if you have, say: n=rand(r), x=rand(±√(r²-n²)), y=rand(±√(r²-x²)) it seems to work.<p>Here&#x27;s a version I extended to three dimensions, designed to be run in the JS console of the original article (it&#x27;ll show up underneath the first sphere):<p><pre><code> new SphereSimulation(document.getElementById(&quot;spheres1&quot;), () =&gt; { const rand = Math.random const root = (x) =&gt; rand() &lt; 0.5 ? Math.sqrt(x) : -Math.sqrt(x) const invdist = (a=0, b=0) =&gt; root(RADIUS*RADIUS - a*a - b*b) const x1 = rand() * invdist() const x2 = rand() * invdist(x1) const y2 = rand() * invdist(x2) const x3 = rand() * invdist(x2, y2) const y3 = rand() * invdist(x3, y2) const z3 = rand() * invdist(x3, y3) return new THREE.Vector3(x3, y3, z3) }) </code></pre> It looks okay to me - am I missing something? Is this a reasonable approach?
评论 #17690039 未加载
评论 #17690013 未加载
评论 #17690633 未加载
hatsunearualmost 7 years ago
So what ends up being the general purpose method for uniformly distributed random numbers in some arbitrary shape? (other than the rejection method of course)<p>If arbitrary shapes always requires the rejection method, what are some &quot;classes&quot; that have easier algorithms (the sphere is obviously included here)
评论 #17695250 未加载
andrew3726almost 7 years ago
This is also very important in many global illumination techniques which requires sampling the hemisphere around a point. As case in point a simple Monte Carlo path tracer uses this to sample random directions, see e.g. [0]. In addition to this there are many &#x27;tricks&#x27; one can use to reduce the variance&#x2F;noise using a differently weighted sampling (to help the converge of MC). A nice technique to get a cosine-weighted hemisphere sampling is to generate a random point on the unit disc and then project them onto the (hemi)sphere. Seems somewhat relevant here.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Path_tracing#Algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Path_tracing#Algorithm</a>
goldenkeyalmost 7 years ago
The method I immediately think of is just based on the relation.<p>Sqrt(x^2 + y^2 +z^2) &lt;= 1 reduces to x^2 + y^2 +z^2 &lt;= 1<p>Generate the random value for x^2, any random value &lt;= 1<p>Now we have y^2 +z^2 &lt;= 1-x^2<p>Generate a random value for y^2, any random value &lt;= 1-x^2<p>Finally generate a random value for z^2, any random value &lt;= 1-x^2-y^2<p>Take the square roots of each number we generated to get the final coordinates.<p>But wait! Theres another step..the most important one! There are 6 orderings of (x,y,z) We must choose a random one because otherwise x will be biased towards higher values compared to y and z.<p>So I kind of lied. All we needed was 3 random values via the subtraction methods above. Then we need to randomly choose their ordering as (#,#,#)<p>This seems like the easiest most trivial solution that would be uniformly distributed.<p>Let me know if I am wrong.
评论 #17690577 未加载
评论 #17691232 未加载
jjgreenalmost 7 years ago
<a href="http:&#x2F;&#x2F;blog.geomblog.org&#x2F;2013&#x2F;01&#x2F;a-sampling-gem-sampling-from-ellp-balls.html" rel="nofollow">http:&#x2F;&#x2F;blog.geomblog.org&#x2F;2013&#x2F;01&#x2F;a-sampling-gem-sampling-fro...</a>
alstangealmost 7 years ago
So which algorithm ended up being fastest?
评论 #17688917 未加载
评论 #17688906 未加载
herbertkanealmost 7 years ago
Section 3 of this paper explains this topic pretty well in N-dimensions:<p><a href="http:&#x2F;&#x2F;jmlr.org&#x2F;papers&#x2F;volume17&#x2F;blaser16a&#x2F;blaser16a.pdf" rel="nofollow">http:&#x2F;&#x2F;jmlr.org&#x2F;papers&#x2F;volume17&#x2F;blaser16a&#x2F;blaser16a.pdf</a><p>To get a random point <i>within</i> the sphere (rather than on the surface of the sphere, as described in the paper) you also need to pick a random distance from the origin to randomly scale the point on the surface.
throwaway313213almost 7 years ago
This is similar to the method used for generating random numbers using Box-Muller transform.<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Box%E2%80%93Muller_transform" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Box%E2%80%93Muller_transform</a>
cjhanksalmost 7 years ago
Maybe a mathematician can correct me. But could you not also model 3D vertices of uniform density into a 1D space and then uniformly sample the 1D space. Of course this may have issues with highly dense objects or very large ones.
评论 #17689906 未加载
Myrmornisalmost 7 years ago
I find it unclear whether the author is aiming for Uniform, or just “looks pretty uniform”.
machiavelli1024almost 7 years ago
Notice how the rejection method becomes useless for higher dimensions.<p><a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Curse_of_dimensionality" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Curse_of_dimensionality</a>
评论 #17690470 未加载
expuextoalmost 7 years ago
I don&#x27;t agree with the way he normalizes the radius in the last 2 methods. I think it should be a square root, not a cubic one. This is because the volume diferential r^2•sin(phi)•d_phi•d_theta•dr is proportional to the square of r. This mistake is also made in a source he cites: <a href="https:&#x2F;&#x2F;math.stackexchange.com&#x2F;questions&#x2F;87230&#x2F;picking-random-points-in-the-volume-of-sphere-with-uniform-probability&#x2F;2872878" rel="nofollow">https:&#x2F;&#x2F;math.stackexchange.com&#x2F;questions&#x2F;87230&#x2F;picking-rando...</a> and I tried to point it out there as well.
评论 #17691294 未加载
评论 #17692379 未加载
评论 #17696110 未加载