diff options
Diffstat (limited to 'src/random.c')
-rw-r--r-- | src/random.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/random.c b/src/random.c index f6cf85e..e2e5641 100644 --- a/src/random.c +++ b/src/random.c @@ -12,3 +12,19 @@ double randn(void) return sqrt(-2*log(u1))*cos(2*M_PI*u2); } + +void rand_sphere(double *dir) +{ + /* Generates a random point on the unit sphere. */ + double u, v, theta, phi; + + u = genrand_real1(); + v = genrand_real1(); + + phi = 2*M_PI*u; + theta = acos(2*v-1); + + dir[0] = sin(theta)*cos(phi); + dir[1] = sin(theta)*sin(phi); + dir[2] = cos(theta); +} |