-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
53 lines (44 loc) · 929 Bytes
/
Copy pathutil.c
File metadata and controls
53 lines (44 loc) · 929 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "util.h"
#include <stdlib.h>
cell_t get_random()
{
return 2.0 * ((cell_t)rand() - (cell_t)RAND_MAX / 2.0) / (cell_t)RAND_MAX;
}
cell_t get_random_bounded(cell_t a, cell_t b)
{
return a + (get_random() / 2.0 + 0.5) * (b - a);
}
void choose_n_indices(size_t n, size_t max_index, size_t* out)
{
for(size_t i = 0; i < n; ++i)
{
out[i] = rand() % max_index;
}
}
void shuffle(size_t* arr, size_t size)
{
if (size <= 1) return;
for (size_t i = 0; i < size - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (size - i) + 1);
size_t temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
cell_t clamp(cell_t in, cell_t min, cell_t max)
{
if(in > max) return max;
if(in < min) return min;
return in;
}
cell_t max(cell_t a, cell_t b)
{
if(a > b) return a;
return b;
}
cell_t absc(cell_t a)
{
if(a < 0) return -a;
return a;
}