# Generating random vectors

• Aug 29th 2013, 03:01 PM
fisian
Generating random vectors
Hey folks,

I need an algorithm to generate random vectors. Restriction on vectors $x=(x_1,\ldots,x_n)$ are as follows:

a) $x_1 + x_2 + \ldots + x_n = 0$ so they lie in an $n-1$ dimensional hyperplane going through zero.
b) $x_i \in [-1,1] \forall i$
c) The density is uniform, e.g. each vector satisfying conditions a) and b) is picked with equal probability.

Example: $(1,-1/2,-1/2)$ and $(0,0,0)$ are both OK as long as they are generated with the same probability.

Thanks:)
• Aug 29th 2013, 03:51 PM
HallsofIvy
Re: Generating random vectors
Use whatever random number generator you like to get values for $x_1$, $x_2$, ..., $x_{n-1}$ between -1 and 1, then calculate $x_n= -x_1- x_2- x_3- \cdot\cdot\cdot - x_{n-1}$.
• Aug 29th 2013, 06:13 PM
fisian
Re: Generating random vectors
Sorry, but your method does not neccessarily guarantee that $x_n \in [-1,1]$ is satisfied, for example if all your draws from $1$ to $n-1$ are equal to one and $n\geq 3$.
• Aug 31st 2013, 04:28 PM
fisian
Re: Generating random vectors
@HallsofIvy any other ideas? I would be really thankful since I think this one is easy and it's just me who's stupid :)