0

Given gunction float frand(), that generates uniformly-distributed random floating point value, in range [0.0, 1.0)

How can I create uniformly-distributed random 3D unit vector, and 4D unit quaternion? (so that a 3D vector lies on a unit sphere, and 4D - on a unit hypersphere, and vectors are uniformly-distributed among correspoiding surface)

xakepp35
  • 2,878
  • 7
  • 26
  • 54

1 Answers1

2

You should pick components of your vector from Gaussian distribution (mean = 0, deviation = 1) using your frand() function and then normalize it.

Another simpler approach is to generate random vector (x, y, z...) as x = frand(); y = frand(); ... and discard it (generate new one), if it is longer than 1. Then normalize this vector. This will eliminate the cases that skew the distribution. I however find this method ugly, because it's unnecessary example of Las Vegas algorithm, which might not terminate.

Check out this answer to similar question: random unit vector in multi-dimensional space

veprolet
  • 361
  • 2
  • 12
  • gaussian distribution has maximum at (0,0,0) so most probably we will get zero-length vector.. normalize it? I am not getting idea in here... As per second, yes, its variand [proposed by G.Marsaglia](https://en.wikipedia.org/wiki/Marsaglia_polar_method) – xakepp35 Aug 21 '19 at 13:50
  • @xakepp35 I'm not sure what you mean by Gaussian distribution having maximum at (0, 0, 0). I'm not talking multivariate Gaussian distribution and even then there seems to be misunderstanding. For each component (e.g. x, y and z in (x, y, z)) you would generate random number from normal distribution independently. Your function `frand()` could be for example used in Box-Muller transformation to generate such number. Then you should normalize the vector by dividing each component by the length of the vector, which will convert any vector to unit vector. – veprolet Aug 21 '19 at 14:31
  • by gaussian(normal) distribution I mean [wiki definition](https://en.wikipedia.org/wiki/Normal_distribution) - If you look at probability graph you will see that its most probably that we will end up with (0,0,0) vector... What do you mean by normalizing it, and how does gaussian normal distribution is related to getting point on sphere surface (not inside sphere, but on its surface!) – xakepp35 Aug 21 '19 at 14:38
  • @xakepp35 It is indeed possible (yet unprobable) to get 0-length vector and this should be accounted for. Most of the time you'll end up with some vector "inside the unit sphere" that has random direction, but also random size. To *normalize* the vector (the act of resizing vector to unit size without changing it's direction) you have to divide it's components by the length of the vector (which you get via Pythagorean formula). Formally the last expression in http://mathworld.wolfram.com/HyperspherePointPicking.html. Also check https://en.wikipedia.org/wiki/Unit_vector about normalization. – veprolet Aug 21 '19 at 15:01
  • For explanation of relation between Gaussian distribution and points on hyperspheres check the comments on the duplicate question. Also remember while analyzing that, that because of *normalization* we don't care about the size of the vector (or the distance of the point from origin as you seem to think about it). – veprolet Aug 21 '19 at 15:14