Friday, May 21, 2010

[from James] polar N-body random generation

For completeness' sake, here's the code I wrote for randomly generating a symmetric disk of 1000 particles in polar co-ordinates with pretty constant number density. I tried a few different cartesian-to-polar conversion formulas, but the others always generated an asymmetric disk; this method always generates a θ between 0 and Π/2, then moves from quadrant to quadrant placing a point in each as it goes.


R=[]
T=[]
i=0
while i < 1000:
j=0
while j < 4:
x=rand.random()
y=rand.random()
r=sqrt(x*x + y*y)
if r <= 1.0:
R.append(r)
T.append(j*pi*0.5 + atan(y/x))
j = j+1
i = i+1

No comments:

Post a Comment