Created
December 10, 2011 01:34
-
-
Save cwillmor/1454204 to your computer and use it in GitHub Desktop.
Random unit vector generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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