Skip to content

Instantly share code, notes, and snippets.

@kayru
Created April 30, 2012 18:22
Show Gist options
  • Save kayru/2560755 to your computer and use it in GitHub Desktop.
Save kayru/2560755 to your computer and use it in GitHub Desktop.
Generate random points inside a sphere
import sys
import random
def GeneratePointsInsideSphere(num_points) :
points = []
for i in range(num_points) :
while True :
x = random.uniform(-1,1)
y = random.uniform(-1,1)
z = random.uniform(-1,1)
if( (x*x+y*y+z*z) <= 1.0 ) :
points.append([x,y,z])
break
return points
if len(sys.argv) > 1 :
points = GeneratePointsInsideSphere(int(sys.argv[1]))
print "float3 points[] = \n{"
for i in range(len(points)) :
print "\tfloat3(%(x).03f, %(y).03f, %(z).03f)," % {"x":points[i][0], "y":points[i][1], "z":points[i][2]}
print "};"
else:
print "Usage: generate_points_inside_sphere.py <num_points>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment