-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
82 lines (59 loc) · 1.49 KB
/
utils.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
utils.c
Miscellaneous mathematical methods used in the elementary cellular automata
simulation engine.
Copyright (c) 2018 andynines
MIT License
*/
// Includes
#include <stdbool.h>
#include <stdlib.h>
#include "utils.h"
// Utility methods
static int absVal(int n) {
// Return the absolute value of an integer argument
if (n < 0) {
n *= (-1);
}
return n;
}
bool binomProb(double p) {
/*
Given some double representing the probability of success of a two-outcome
event, return a boolean representing the success of this event during a
single trial. True means success; false means failure.
*/
double zTest;
zTest = (double) rand() / RAND_MAX;
return (zTest <= p);
}
int min(int a, int b) {
/*
Given two integer arguments, return back the smaller of those two values.
*/
if (a < b) {
return a;
} else {
return b;
}
}
bool wrap(int* n, int nMax) {
/*
Given a pointer to an integer and a maximum value for this number, test to
see if this number is within the inclusive range between 0 and the specified
maximum. If it is outside this range, wrap the number back into it using
modulo and return true. Otherwise, return false.
*/
int range;
int absVal(int n);
range = nMax + 1;
if (*n > nMax) {
*n %= range;
return true;
} else if (*n < 0) {
*n = range - (absVal(*n) % range);
return true;
} else {
return false;
}
}