Skip to content

Instantly share code, notes, and snippets.

@jorgecarleitao
Created May 21, 2014 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgecarleitao/a7ef61d4d918ee8059d6 to your computer and use it in GitHub Desktop.
Save jorgecarleitao/a7ef61d4d918ee8059d6 to your computer and use it in GitHub Desktop.
Generates a unitary random vector in D dimensions
std::vector<double> unitaryVector(unsigned int dimension)
{
std::vector<double> vector(dimension);
double norm = 0;
for (unsigned int d = 0; d < dimension; d++)
{
vector[d] = grandom(); // RNG call for a gaussian with mean=0 and sigma=1
norm += vector[d]*vector[d];
}
norm = sqrt(norm);
for (size_t d = 0; d < dimension; d++)
{
vector[d] /= norm;
}
return vector;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment