aboutsummaryrefslogtreecommitdiff
path: root/src/random.c
blob: e2e56418e6cbee831ac25d7f0a424b1b4bd4b249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "random.h"
#include <math.h>

double randn(void)
{
    /* Generates a random number from a normal distribution using the
     * Box-Muller transform. */
    double u1, u2;

    u1 = genrand_real1();
    u2 = genrand_real1();

    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);
}