Random 4D vector

330 Views Asked by At

I'd like to generate random 4D vectors. Their length must be 1.

A random 2D vector can be created like so:

vec[0] = sin(rand1 * PI * 2)
vec[1] = cos(rand1 * PI * 2)

A random 3D vector can be created like so:

vec[0] = sin(rand1 * PI * 2) * cos(rand2 * PI * 2);
vec[1] = cos(rand1 * PI * 2) * cos(rand2 * PI * 2);
vec[2] = sin(rand2 * PI * 2);

(rand1 and rand2 are random numbers between 0 and 1)

How can you make a random 4D vector in the same manner. Would it be possible to some how combine two 2D vectors?

2

There are 2 best solutions below

0
On BEST ANSWER

There are many ways to do this. One is given here: http://mathworld.wolfram.com/HyperspherePointPicking.html

Summary: Generate $x_1,\dots,x_4$ using the standard normal distribution (often built-in) and normalize the vector $[x_1,\dots,x_4]$ to have unit length.

1
On

Generalize from 3D:

vec[0] = sin(rand1 * PI * 2) * cos(rand2 * PI * 2) * cos(rand2 * PI * 2);
vec[1] = cos(rand1 * PI * 2) * cos(rand2 * PI * 2) * cos(rand2 * PI * 2);
vec[2] = sin(rand2 * PI * 2) * cos(rand3 * PI * 2);
vec[3] = sin(rand3 * PI * 2);