Skip to content

Instantly share code, notes, and snippets.

@cwillmor
Created December 10, 2011 01:34
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 cwillmor/1454204 to your computer and use it in GitHub Desktop.
Save cwillmor/1454204 to your computer and use it in GitHub Desktop.
Random unit vector generation
import random
import math
def random_unit_vector(n):
"""Return the components of a random unit vector in 'n' dimensions."""
result = [random.gauss(0, 1) for i in xrange(n)]
norm = math.sqrt(sum(x**2 for x in result))
result = [x / norm for x in result]
return result
if __name__ == '__main__':
print random_unit_vector(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment