Created
July 19, 2020 19:37
-
-
Save cosinekitty/16144bd96b9b7f74368b81887b2132c0 to your computer and use it in GitHub Desktop.
Python function for generating unbiased direction unit vectors
This file contains 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 math | |
import numpy | |
def randomvector(n): | |
components = [numpy.random.normal() for i in range(n)] | |
r = math.sqrt(sum(x*x for x in components)) | |
v = [x/r for x in components] | |
return v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment