Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created July 19, 2020 19:37
Show Gist options
  • Save cosinekitty/16144bd96b9b7f74368b81887b2132c0 to your computer and use it in GitHub Desktop.
Save cosinekitty/16144bd96b9b7f74368b81887b2132c0 to your computer and use it in GitHub Desktop.
Python function for generating unbiased direction unit vectors
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