17 ms·
How to generate uniformly random points on n-spheres and in n-balls
- JadoJodo 3y agoI'm definitely not at all qualified to talk about this, but... aren't "uniform" and "random" antonyms?
- icoder 3y agoThe article explains this quite early on, as I understand it, it means that every spot has the same chance of being picked.
- bqmjjx0kac 3y ago"Uniform" here indicates that all values have an equal probability of being picked. The distribution is flat.
- pvg 3y agoNo, 'uniform' refers to the distribution, which need not be uniform, e.g. https://numpy.org/doc/stable/reference/random/generated/numpy.random.normal.html https://numpy.org/doc/stable/reference/random/generated/nump... Or in god's own words (TAOCP section 3.4): Applications of random numbers often call for other kinds of distributions, however; for example, if we want to make a random choice from among k alternatives, we want a random integer between 1 and k. If some simulation process calls for a random waiting time between occurrences of independent events, a random number with the exponential distribution is desired. Sometimes we don't even want random numbers — we want a random permutation (a random arrangement of n objects) or a random combination (a random choice of k objects from a collection of n). In principle, any of these other random quantities can be obtained from the uniform deviates U0, U1, U2, ...; people have devised a number of important "random tricks" for the efficient transformation of uniform deviates. A study of these techniques also gives us insight into the proper use of random numbers in any Monte Carlo application.
- jerf 3y ago"All distributions are uniform" is one of the two cardinal crimes of a school level of statistics understanding, the other being "all probabilities are independent".
- deleted 3y ago[deleted]
- wodenokoto 3y agoNo. I would go so far as to say, if it’s not uniformly random, it isn’t random. My reasoning is, if you are counting cards in a game, you are giving yourself an advantage. But what really happens is that from your point of view cards will be drawn less and less uniformly randomly. Or put in another way, if you know the distribution is normal, you can bet on the the result being near the center and come out on top, but if it’s uniformly random distribution all hopes are out.
- olliej 3y agoThere are plenty of things that are random, that are not uniformly random. The hardware RNGs that are used for physically based RNG are not uniform, including the great lava lamp wall or geiger counters, etc. The problem is if you are doing something that requires uniform random values, and your random source is not uniform, then things will go wrong.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- kelseyfrog 3y agoBy that criteria, wouldn't a discrete distribution with infinite support be even more random? Finite support somehow seems less random because you can still say something interesting about its mean and bounds in the same way you can say something about the mean and variance of a normal distribution.
- zamadatix 3y agoRandom (in statistics) means you cannot conclude the next value ahead of time. That's different than saying the next value is uniformly likely to be any value and the two concepts are actually orthogonal. E.g. If I had a bag with 99 red balls and 1 blue ball then randomly selected one you may assume there is a high chance there will be a red ball, based on the odds, but you aren't able to actually know which ball will be next until it's drawn.
- olliej 3y agoNope, uniform is a description of the probabilities of a random distribution. The easiest example most people are aware of in practice is throwing two 6 sided dice vs one 12 sided dice. The outcome of both is random, but people seem to know fairly intuitively that the two dice case produces is more likely to produce middle values than extreme ones, while the single 12 sided dice doesn't have that behavior.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- frankfrank13 3y agoI actually needed this at work once! We needed to fuzz peoples address in a mapped view for analytics, without revealing PII. It ended up never being shipped, but we needed to fuzz geographic data and the thinking was like: 1. Truncate your lat longs to some arbitrary decimal place (this is very very stupid, you end up with grid lines [1]) 2. The above method ^^ but everyone tries basically doing like random angle + random length along angle, which doesn't generate uniform results in a circle as the article mentions[2]. So then you try generating points in a box that encloses your circle, and rejecting anything outside the circle, but that smells bad. So you do some googling and find method 6 listed in the article (Good! and Fast!) 3. Realize that fuzzing points is stupid, what you really want is to view points in aggregate anyways, so you try heat maps [1]: Ok you always end up with grid lines, but truncating to like 1-6 decimal places produces very obvious grid lines to the human eye [2]: Try this in pyplot! You'll see right away with ~100 points its not uniform in the way you expect
- bagels 3y agoI needed something related, n roughly evenly distributed points on the surface of a sphere. Ended up using a Fibonacci spiral. https://extremelearning.com.au/how-to-evenly-distribute-points-on-a-sphere-more-effectively-than-the-canonical-fibonacci-lattice/ https://extremelearning.com.au/how-to-evenly-distribute-poin...
- milleramp 3y agoHey me too, very interesting problem. I also used this method as well. https://www.sciencedirect.com/science/article/abs/pii/S0010465518301292 https://www.sciencedirect.com/science/article/abs/pii/S00104...
- alanbernstein 3y agoForce directed layout is my favorite for this.
- fjkdlsjflkds 3y ago...but then the algorithmic complexity goes from O(N) to O(N^2), or at least O(N log N), since points have to interact with each other. (N denotes the number of points you need to generate)
- olliej 3y agoI was always surprised at how easily you get biased sampling when generating random points despite all input - something I often saw students do was essentially normalize({2rand() - 1, 2rand() - 1, 2rand() - 1}) or variations of that (where rand() is "good" not the literal rand(3)), and there are numerous other ways that are more subtly wrong. IIRC the nominally correct way for a sphere specifically is something like a=rand() b=rand() and the random point is something* like { cos(a)sin(b), sin(b), cos(a)cos(b) }[1] I think the best illustration of "reasonable choices of what random values should be used leading to biased results" is Bertrand's paradox which I was introduced to via numberphile/3blue1brown: https://www.youtube.com/watch?v=mZBwsm6B280 https://www.youtube.com/watch?v=mZBwsm6B280 and am just glad that nothing I have ever needed random sampling for has ever been important :D [1] please don't use this blindly, I'm really just going off very old recollection, if you need it google random sphere sampling :D
- hinkley 3y agoI used to think “normal distributions are everywhere” but the more math and science I watch on YouTube the more the central limit theorem pops up. It’s the CLT that’s everywhere, it just brings normal distribution as it’s +1.
- richrichie 3y agoIndeed. The fundamental insight behind CLT i.e. sample average is normally distributed even when population distribution is not normal is intuitive, yet the the theorem is magical.
- hinkley 3y agoWhen you have more than one variable, the outcomes get clumpy.
- enthdegree 3y agoI find it hard to believe the dropped coordinates approaches were first noticed in 2010 and proven in 2017.
- kloch 3y agoI find it amusing that you need a normal distribution of scalars to generate a uniform distribution of vectors on an n-sphere.
- ykonstant 3y agoI wonder if CS people have studied the method of Lubotzky, Phillips and Sarnak to produce well-distributed points on spheres: https://onlinelibrary.wiley.com/doi/abs/10.1002/cpa.3160390710 https://onlinelibrary.wiley.com/doi/abs/10.1002/cpa.31603907...
- montefischer 3y agoIf you're interested in things like this, you might like to read the paper of Diaconis, Holmes, and Shashahani on sampling from a manifold. https://arxiv.org/abs/1206.6913 https://arxiv.org/abs/1206.6913
- xLaszlo 3y agoIf you are looking for low discrepancy (fills the space "smoothly") quasy random numbers check out Sobol and Niederreiter sequences.
- IshKebab 3y agoNo don't. Look at plastic numbers instead. They are much better.
- nighthawk454 3y agoSee also this method based on optimizing nearest-neighbor distances after initializing with fibonacci spiral: https://extremelearning.com.au/how-to-evenly-distribute-points-on-a-sphere-more-effectively-than-the-canonical-fibonacci-lattice/ https://extremelearning.com.au/how-to-evenly-distribute-poin...
- dmd 3y agoAs an RA, before starting grad school, I hacked together some code to choose a random point on a sphere, for some psychophysics experiment I was doing. Fortunately, I never used it for anything, because I made the classic naive mistake of simply choosing a random theta in 0,2pi and phi in -pi,pi, which ends up with points biased towards the poles. Somehow *12 years later* my subconscious flagged it up and I woke up in the middle of the night realizing the issue. Even though I'd never revisited it since then! https://github.com/dmd/thesis/commit/bff319690188a62a79821aa404b82e9835bf7499 https://github.com/dmd/thesis/commit/bff319690188a62a79821aa...
- marcodiego 3y agoA friend of mine made this mistake a long time ago when sampling points in a circle. He was baffled and couldn't understand why there was concentration of points around some areas of the circle. My explanation: draw a square around this circle; trace a horizontal line passing along its center; now, draw a diagonal line; can you see it is more likely that a randomly sampled point is closer to the diagonal than to horizontal line? He said "sure!" and I asked "why?" to what he promptly answered "because its longer.". He then asked what he should do about it, I simply said: "just ignore the points that are too far". He immediately understood that points inside the square but outside the disk were the reason for the concentration of the points.
- xanderlewis 3y ago> simply choosing a random theta in 0,2pi and phi in -pi,pi, which ends up with points biased towards the poles. Am I right in thinking that it’s because circles of constant theta on the sphere contain the same ‘expected’ number of points (since phi is uniformly distributed), and these circles get smaller (and in the limit, shrink to a point) as one travels towards the poles — hence, higher density of points…? …makes me realise that spherical polar coordinates, as natural as they seem, aren’t in some way homogeneous in the sense that whilst the mapping (theta, phi) —> (x, y, z) is continuous, it isn’t uniformly so… or something like that. I don’t know enough (possibly differential) geometry to articulate it properly, but it feels like the problem of getting genuinely uniform sampling from a sphere (or indeed any manifold) is somewhat equivalent to having a ‘nice’ coordinate system (one that respects the symmetry of the sphere). Basically, polar coordinates are prejudiced and treat points differently depending on how close to the poles they are. By definition, our sampling in the domain space (two intervals) is uniform; the problem comes when we project through a coordinate system that doesn’t respect this. Which better solution did you use? I’m having trouble reading your code on my current device.
- atum47 3y agoSome years ago I saw a blue and red image that caught my attention. The red dots seemed to be floating around the blue ones. I later found out that has a name - Chromostereopsis [1]. So I decided to make my own image and needed to distribute some points in a circle, this how I did it [2]. [1] - https://en.wikipedia.org/wiki/Chromostereopsis https://en.wikipedia.org/wiki/Chromostereopsis [2] - https://jsfiddle.net/victorqribeiro/vxf2ajzm/48/ https://jsfiddle.net/victorqribeiro/vxf2ajzm/48/
- pugworthy 3y ago2-ball (disk) distribution is kind of interesting in gaming when simulating gun projectile hit locations. If the circle represents the area in which a simulated projectile will hit, you probably don't want a truly random distribution of points but instead have a bias towards the middle of the circle. A real gun shot multiple times at a fixed target will probably (assuming perfect aim but some variation on every shot) have more shots hit the middle of the pattern than the edges. Some early Valve shot code actually had a purely random distribution, but at some point an alternate version got written and shared at https://developer.valvesoftware.com/wiki/CShotManipulator https://developer.valvesoftware.com/wiki/CShotManipulator Ironically the biased version is based on a pretty simple method that in fact people sometimes get wrong when they want a truly random point distribution in a circle. Just doing a random radius and theta will lead to a biased distribution. Wolfram Mathworld has a good writeup on it at https://mathworld.wolfram.com/DiskPointPicking.html https://mathworld.wolfram.com/DiskPointPicking.html
- xanderlewis 3y ago> truly random distribution > purely random distribution Nitpick: you don’t mean random; you mean uniform.
- pugworthy 3y agoGame players live and die (virtually) by the fabled RNG and pray to RNGesus even. So though mathematically uniform is the right word, gamer-wise, random is a pretty good word to use.
- porphyra 3y agoAnother way to generate uniformly random points on a 2D disk that the author forgot to mention: let A be an n x n complex matrix whose elements are iid copies of a fixed random variable with unit variance. Let lambda_i be its ith eigenvalue, and let x_i = 1/sqrt(n) real(lambda_i) and y_i = 1/sqrt(n) imag(lambda_i). As n approaches infinity, the distribution of x, y approaches almost certainly to the uniform distribution over the unit disk. Tao, T., Vu, V., and Krishnapur, M. (2010) Random matrices: universality of ESDs and the circular law. The Annals of Probability. 38(5) 2023-2065.
- tiffanyh 3y agoOff topic: isn't "uniformly random" a contradiction of terms?
- kadoban 3y agoNo. Random just means you can't predict what value will be chosen, but doesn't tell you how likely different values are. If I roll a 12-sided die, that's random. If I roll two 6-sided dice and add the result, that's also random, but it has a different distribution of values. The two dice verison, there's one way to get a result of 2, but _several_ ways to get 7. You'll get 7 way more often than you'll get 2. The one-die version each outcome is equally likely. You're exactly as likely to get 2 as you are to get 7 or any other value in the range of possibilities. The one-die version is a uniform distribution. The two-dice version is not uniform.
- esafak 3y agoRandom is not precise; it does specify a distribution, though uniform is commonly assumed. A better term is "uniformly distributed".
- stefanka 3y agoI encountered the need for uniform random points on hyperspheres, and found this solution (with Python code) very helpful: https://stackoverflow.com/a/59279721 https://stackoverflow.com/a/59279721. Currently, I am porting my codebase to Rust and did that part over the weekend, so if anyone is interested in this exact implementation, I'd willing to share it (as a crate if necessary).
- frumiousirc 3y agoThe linked SO question is not about uniform random points. The poster explicitly excludes answers involving uniform random distribution on a hypersphere.
- renonce 3y agoI learned the easiest way to do d-dimension sampling in Foundations of Data Science: see https://news.ycombinator.com/item?id=34575637 https://news.ycombinator.com/item?id=34575637 or https://www.cs.cornell.edu/jeh/book.pdf?file=book.pdf https://www.cs.cornell.edu/jeh/book.pdf?file=book.pdf I don't think it's a good idea to introduce over 20 different methods before talking about the correct one that works for any number of dimensions, say n, and the reason behind its correctness is very obvious: * Generate a random vector by sampling n standard normal distributions: `vector = np.random.randn(n)` * Key step: show that the vector has a uniform direction. The proof is as follows: you look at the probability density function of a normal distribution, which is `p(x)=1/sqrt(2pi)*exp(-x^2/2)`, and the probability density function of the vector is the product of all these densities of its individual dimensions. Now the product `p(x1)p(x2)...p(xn)=1/sqrt(2pi)^n*exp(-(x1^2+x2^2+...+xn^2)/2)` since `exp(a)exp(b)=exp(a+b)` due to power functions. Now it's easy to see that probability density is invariant to vector length, which means the vector has uniform probability for any specific value of x1^2+x2^2+...+xn^2. Whatever rotation you apply after sampling this vector, since rotation preserves x1^2+x2^2+...+xn^2 by definition, you get the exact same probability density function and therefore the same distribution of vectors. * Now that the direction of the vector is uniformly sampled, decide the radius separately: for n-sphere the radius is just 1, and for n-ball the volume of the radius is proportional to the nth power of the radius, so you sample a uniform number from [0,1] as the volume and take the nth root as the radius: `radius = np.random.uniform(0, 1)*(1/n)` * Normalize the vector to the radius: `vector = vector / np.sqrt((vector*2).sum()) * radius` I think Section 2 of this book provides a much better perspective on the problem of generating uniform random points, since it also provides intuition behind the geometry of high dimensions and properties of the unit ball, etc.
- FooBarBizBazz 3y agoThis is the way.
- 0-_-0 3y agoI can't believe I didn't already know this
- ackbar03 3y agoIs this the same as uniform SO(N) sampling? I had to do something similar like this for some deep learning training method I was working on. I gave up