6
Generate a Gauss-distributed (pseudo)random number.
7
"By Box and Muller, and recommended by Knuth".
8
@return A Gauss-distributed random number.
13
static gdouble V1, V2, S;
14
static gint phase = 0;
19
gdouble U1 = g_rand_double(rand_generator);
20
gdouble U2 = g_rand_double(rand_generator);
24
S = V1 * V1 + V2 * V2;
25
} while(S >= 1 || S == 0);
27
X = V1 * sqrt(-2 * log(S) / S);
29
X = V2 * sqrt(-2 * log(S) / S);
37
Generate a Gauss-distributed random number within given boundaries
38
using math_gaussrand().
39
Expectation value of the distribution is (upper + lower) / 2,
40
the variance is so that the number is between the boundaries with probability
41
99,7 %. If the number isn't between the boundaries, we cut off.
42
@param lower Lower cutoff boundary.
43
@param upper Upper cutoff boundary.
44
@return A Gauss-distributed number
47
math_gauss_dist(gdouble lower, gdouble upper)
51
result = (upper - lower) / 6 * math_gaussrand()
52
+ (upper + lower) / 2;
64
Get a certain part of an integer number.
66
If 'place' is between 1 and 9, the 'place'th digit beginning
67
from the right is returned, e.g. if the number = 1234 and
68
place = 2, the function returns 3.
70
If 'place' is between 10 and 19, say 10 + x, the first
71
'x' digits are returned, e.g. number = 8765 and place = 12 leads to
74
If 'place' is between 20 and 29, say 20 + x, the last
75
'x' digits are returned, e.g. number = 4869 and place = 22
76
leads to return value 69.
78
@param value The number which gets scrutinized.
79
@param place The number telling the function which part of 'value' to return.
80
@return A part of the integer 'value'.
83
math_get_place(gint value, gint place)
86
return (value % (gint)powf(10, place) -
87
value % (gint)powf(10, place - 1)) /
88
(gint)powf(10, place - 1);
92
while(value >= (gint)powf(10, place % 10))
93
value = (value - value % 10) / 10;
98
return value % (gint)powf(10, place % 10);
102
Round an integer with given precision.
104
If places > 0, round with precision 'places', e.g.
105
number = 124566 and places = 2 leads to return value
108
If places < 0, precision is length of 'number' minus
109
'places', e.g. number = 654987 and places = -2 leads to return
112
@param number The number to be rounded.
113
@param places The precision.
114
@return The rounded integer.
117
math_round_integer(gint number, gint places)
120
gfloat copy = (gfloat)number;
123
return (gint)rint( (gfloat)number / powf(10, places) ) *
132
return (gint)rint( (gfloat)number / powf(10, length + places) ) *
133
powf(10, length + places);
136
/** Generate a permutation of integers and write it to 'array'.
137
@param array The integer array we store the permutation in.
138
It must have size at least end - start - 1.
139
@param start The integer to start with.
140
@param end The integer to end with. */
142
math_generate_permutation(gint *array, gint start, gint end)
146
for(i = start; i < end + 1; i++)
147
array[i - start] = i;
149
for(i=0;i<end - start;i++)
150
misc_swap_int(&array[i], &array[math_rndi(i, end - start)]);
153
/** This function tells us how many teams from 'number' teams
154
have to be left away to obtain a power of 2. */
156
math_get_bye_len(gint number)
161
if((gint)powf(2, i) >= number)
164
return (gint)powf(2, i) - number;
167
/** Return the sum of the integers in the array.
168
@param array The integer array.
169
@param max The size of the array.
170
@return The sum of all the integers in the array. */
172
math_sum_int_array(const gint *array, gint max)