### Random vector

I need a random unit vectors in n-dimensional space. Any idea how to build it in C++?
What do you mean by "random unit vector"?
Do you mean a unit vector that has random values?
From my current (very little) understanding, you would probably need to use arrays/vectors.
Also, depending on what you plan to do with your "random unit vector" will influence implementation details.
Sorry, I was not clear. I did not meant vector of C++. I imply the geometric vector (a geometric object that has magnitude and direction). "Unit" means that the magnitude is equal to one. The direction should be random and isotropic (any direction should be equally possible). For example in 2D a random unit vector (x1, x2) can be build like that:
 ``1234567891011`` ``````#include #include ... double x1, x2, phi, pi; pi = 4.0*atan(1.0); phi = rand()/double(RAND_MAX); phi *= 2.0*pi; x1 = sin(phi); x2 = cos(phi);``````

The question is how to do similar things generally in 3D or in higher dimensional space?
For example, for the three dimensional Cartesian coordinate system:

a. Generate a random direction vector (0,0,0) tp (x,y,z) where x, y, z are random
b. Scale it to the unit vector (0,0,0) tp ( x^ , y^ , z^ )
c. If required, shift the origin to a random location ( x1, y1, z1 )

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include #include #include #include #include #include #define WARM_UP_ENGINE using vector = std::tuple ; const auto x = [] ( vector v ) { return std::get<0>(v) ; } ; const auto y = [] ( vector v ) { return std::get<1>(v) ; } ; const auto z = [] ( vector v ) { return std::get<2>(v) ; } ; double norm( vector v ) { return std::sqrt( x(v)*x(v) + y(v)*y(v) + z(v)*z(v) ) ; } vector random_vector() { #ifdef WARM_UP_ENGINE // if high quality pseudo-randomness is criticsl static int sseq[ std::mt19937::state_size ] ; const static bool once = ( std::srand( std::time(nullptr) ), std::generate( std::begin(sseq), std::end(sseq), std::rand ), true ) ; static std::seed_seq seed_seq( std::begin(sseq), std::end(sseq) ) ; static std::mt19937 twister(seed_seq) ; #else // no warm up is required, should be adequate for normal use static std::mt19937 twister( std::time(0) ) ; #endif // WARM_UP_ENGINE static std::uniform_real_distribution distr( -1000, 1000 ) ; return vector( distr(twister), distr(twister), distr(twister) ) ; } vector random_unit_vector() { auto v = random_vector() ; constexpr double epsilon = 0.01 ; double m = norm(v) ; if( m > epsilon ) return vector( x(v)/m, y(v)/m, z(v)/m ) ; else return random_unit_vector() ; } int main() { std::cout << std::fixed << std::setprecision(3) ; for( int i = 0 ; i < 10 ; ++i ) { auto v = random_unit_vector() ; std::cout << std::showpos << "(" << x(v) << ',' << y(v) << ',' << z(v) << ") " ; // move the random unit vector to a random location static std::mt19937 twister( std::time(0) ) ; static std::uniform_real_distribution distr( -10, 10 ) ; double x1 = distr(twister), y1 = distr(twister), z1 = distr(twister) ; double x2 = x1 + x(v), y2 = y1 + y(v), z2 = z1+z(v) ; std::cout << std::showpos << " (" << x1 << ',' << y1 << ',' << z1 << ')' << " ----> (" << x2 << ',' << y2 << ',' << z2 << ")\n\n" ; } }``````

http://ideone.com/13p4VJ