3
3
* math-util.c - Small math utilities shared by other modules
5
5
* Fyre - rendering and interactive exploration of chaotic functions
6
* Copyright (C) 2004-2005 David Trowbridge and Micah Dowty
6
* Copyright (C) 2004-2006 David Trowbridge and Micah Dowty
8
8
* This program is free software; you can redistribute it and/or
9
9
* modify it under the terms of the GNU General Public License
29
float uniform_variate() {
28
/* It's much faster to use our own global g_rand, rather than
29
* relying on the g_random_* family of functions. Those functions
30
* are thread-safe, and the locking around that shared g_rand can
31
* take a very significant amount of CPU. We don't need thread-safe
32
* random variates yet.
34
static GRand* global_random = NULL;
37
global_random = g_rand_new_with_seed(time(NULL));
40
double uniform_variate() {
30
41
/* A uniform random variate between 0 and 1 */
31
return g_random_double();
34
float normal_variate() {
35
/* A unit-normal random variate, implemented with the Box-Muller method */
36
return sqrt(-2*log(g_random_double())) * cos(g_random_double() * (2*M_PI));
42
return g_rand_double(global_random);
45
void normal_variate_pair(double *a, double *b) {
46
/* Produce a pair of values with a standard normal distribution,
47
* using the Polar Box-Mueller method.
52
x = uniform_variate();
54
y = uniform_variate();
57
/* Squared radius. The vector must be nonzero,
58
* and it must lie within the unit circle.
61
} while (r2 > 1 || r2 == 0);
63
/* Perform the Box-Mueller transform on each component */
64
m = sqrt(-2.0 * log(r2) / r2);
69
int int_variate(int minimum, int maximum) {
70
return g_rand_int_range(global_random, minimum, maximum);
39
73
int find_upper_pow2(int x) {