/* Copyright (c) 2019, Anthony Latorre * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "random.h" #include 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); }