-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtility.cpp
More file actions
28 lines (26 loc) · 896 Bytes
/
Copy pathUtility.cpp
File metadata and controls
28 lines (26 loc) · 896 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
// this is used by the Utility methods
#include <vector>
#include <cstdlib>
#include <ctime>
namespace Utility {
int seed = std::time(0);
std::vector<unsigned int> boollistToIndices(bool* btable, const size_t size) {
// converts a bool array { true, false, true } into
// an array of int indices { 0, 2 } where it's true
std::vector<unsigned int> itable;
for (size_t i = 0; i < size; i++) {
if (btable[i] == true) { itable.push_back(i); }
}
return itable;
}
int randint(unsigned int min, unsigned int max) {
std::srand(seed++);
int n = rand() % (max - min); // max - min : the modulo operator
return n + min;
}
int randchoice(bool* btable, const size_t size) {
std::vector<unsigned int> itable = boollistToIndices(btable, size);
unsigned int i = randint(0, itable.size()); // random number from table range
return itable[i]; // random indice from the table
}
}