Skip to content

Commit

Permalink
Use standard random library
Browse files Browse the repository at this point in the history
Addresses compiler warnings and use of deprecated functions.
  • Loading branch information
SuprDewd committed Dec 30, 2024
1 parent 760d485 commit e8a38a5
Show file tree
Hide file tree
Showing 35 changed files with 105 additions and 113 deletions.
12 changes: 6 additions & 6 deletions code/data-structures/avl_tree.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ void test() {
assert_equal(0, t1.size());

for (int i = 0; i < cnt; i++) {
int n = rand() % range;
int n = rng() % range;
avl_tree<int>::node *p = t1.insert(n);
assert_equal(n, p->item);
t2.insert(n);
assert_equal((int)size(t2), (int)size(t1));

int n1 = rand() % range;
int n1 = rng() % range;
avl_tree<int>::node *b = t1.find(n1);
if (b) assert_equal(n1, b->item);
assert_equal(b == NULL, t2.find(n1) == t2.end());

int n2 = rand() % range;
int n2 = rng() % range;
t1.erase(n2);
t2.erase(n2);
assert_equal((int)size(t2), (int)size(t1));
Expand All @@ -33,18 +33,18 @@ void test() {
assert_equal(0, t1.size());

for (int i = 0; i < cnt; i++) {
int n = rand() % range;
int n = rng() % range;
avl_tree<int>::node *p = t1.insert(n);
assert_equal(n, p->item);
t2.insert(n);
assert_equal((int)size(t2), (int)size(t1));

int n1 = rand() % range;
int n1 = rng() % range;
avl_tree<int>::node *b = t1.find(n1);
if (b) assert_equal(n1, b->item);
assert_equal(b == NULL, t2.find(n1) == t2.end());

int n2 = rand() % range;
int n2 = rng() % range;
t1.erase(n2);
t2.erase(n2);
assert_equal((int)size(t2), (int)size(t1));
Expand Down
8 changes: 4 additions & 4 deletions code/data-structures/kd_tree.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void randomize_pt() {
hi = 10;

for (int i = 0; i < CURK; i++) {
pt[i] = static_cast<double>(rand()) / RAND_MAX * (hi - lo) + lo;
pt[i] = uniform_real_distribution(lo, hi)(rng);
pt[i] = round(pt[i] * 3.0) / 3.0;
}
}
Expand All @@ -80,7 +80,7 @@ void test() {
for (int t = 0; t < 100; t++) {
// printf("%d\n", t);

int startcnt = rand() % 1000;
int startcnt = rng() % 1000;

vector<kd_tree<CURK>::pt> pts1(startcnt);
vector<naive_kd_tree<CURK>::point> pts2(startcnt);
Expand Down Expand Up @@ -112,7 +112,7 @@ void test() {
int ops = 1000;
int found = 0;
for (int cop = 0; cop < ops; cop++) {
int op = rand() % 3;
int op = rng() % 3;

if (op == 0) {
// insert
Expand Down Expand Up @@ -147,7 +147,7 @@ void test() {
} else if (op == 2) {
// nearest neighbour
randomize_pt();
bool allow_same = rand() % 2 == 0;
bool allow_same = rng() % 2 == 0;
// bool allow_same = true;
#if TREE1
pair<kd_tree<CURK>::pt, bool> a = tree1.nearest_neighbour(kd_tree<CURK>::pt(pt), allow_same);
Expand Down
8 changes: 4 additions & 4 deletions code/data-structures/misof_tree.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ void test() {
avl_tree<int> t2;

for (int i = 0; i < 1000000; i++) {
int op = rand() % 3;
int op = rng() % 3;

if (op == 0) {
int x = rand() % (1 << BITS);
int x = rng() % (1 << BITS);

// printf("insert %d\n", x);

Expand All @@ -20,14 +20,14 @@ void test() {
else i--;

} else if (op == 1 && size(t2) > 0) {
int x = t2.nth(rand() % size(t2))->item;
int x = t2.nth(rng() % size(t2))->item;

// printf("erase %d\n", x);

t1.erase(x);
t2.erase(x);
} else if (op == 2 && size(t2) > 0) {
int n = rand() % size(t2);
int n = rng() % size(t2);

// printf("nth %d: %d, %d\n", n, t2.nth(n)->item, t1.nth(n));

Expand Down
2 changes: 1 addition & 1 deletion code/data-structures/sparse_table.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct sparse_table_slow {
void test() {
rep(n,0,257) {
vi arr(n);
rep(i,0,n) arr[i] = rand() % 1000000000;
rep(i,0,n) arr[i] = rng() % 1000000000;

sparse_table_slow A(arr);
sparse_table B(arr);
Expand Down
2 changes: 1 addition & 1 deletion code/geometry/welzl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pair<point,double> welzl() {
mx = d, res = (wR[i] + wR[j]) / 2.0;
return make_pair(res, mx/2.0); }
return circumcircle(wR[0], wR[1], wR[2]); }
swap(wP[rand() % wP.size()], wP.back());
swap(wP[rng() % wP.size()], wP.back());
point res = wP.back(); wP.pop_back();
pair<point,double> D = welzl();
if (abs(res - D.first) > D.second + EPS) {
Expand Down
4 changes: 2 additions & 2 deletions code/graph/bipartite_matching.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void test_rand(int N, int M) {
for(int i = 0; i < N; i++) {
g.add_edge(SOURCE, i, 1, 0);
for(int j = 0; j < M; j++) {
if(rand() % 2) {
if(rng() % 2) {
// cout << i << " -> " << j << endl;
g.add_edge(i, N + j, 1, 0);
adj[i].push_back(j);
Expand Down Expand Up @@ -89,7 +89,7 @@ void test() {
test_1();
int TESTS = 1000, MAX_N = 50, MAX_M = 50;
for(int i = 0; i < TESTS; ++i) {
test_rand(rand() % MAX_N, rand() % MAX_M);
test_rand(rng() % MAX_N, rng() % MAX_M);
}
}
// vim: cc=60 ts=2 sts=2 sw=2:
8 changes: 4 additions & 4 deletions code/graph/bipartite_mvc.test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* Field testing: Kattis sensor */
void test() {
rep(ite,0,10000) {
int n = rand() % 400 + 1,
m = rand() % 400 + 1;
int k = rand() % n + 1;
int n = rng() % 400 + 1,
m = rng() % 400 + 1;
int k = rng() % n + 1;
vii es;
rep(i,0,n) {
rep(j,0,m) {
if (rand() % k == 0) {
if (rng() % k == 0) {
es.push_back(ii(i,j));
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/graph/blossom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ vi find_augmenting_path(const vector<vi> &adj,const vi &m){
vii max_matching(const vector<vi> &adj) {
vi m(size(adj), -1), ap; vii res, es;
rep(i,0,size(adj)) iter(it,adj[i]) es.emplace_back(i,*it);
random_shuffle(es.begin(), es.end());
shuffle(es.begin(), es.end(), rng);
iter(it,es) if (m[it->first] == -1 && m[it->second] == -1)
m[it->first] = it->second, m[it->second] = it->first;
do { ap = find_augmenting_path(adj, m);
Expand Down
6 changes: 3 additions & 3 deletions code/graph/dinic.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void test1() {

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int cap = rand() % MAX + 1;
int cap = rng() % MAX + 1;
mf.add_edge(i, j, cap);
mf2.add_edge(i, j, cap);
}
Expand All @@ -34,7 +34,7 @@ void test2() {

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int cap = rand() % MAX + 1;
int cap = rng() % MAX + 1;
mf.add_edge(i, j, cap, cap);
mf2.add_edge(i, j, cap, cap);
}
Expand All @@ -57,7 +57,7 @@ void test3() {

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int cap = rand() % MAX + 1;
int cap = rng() % MAX + 1;
mf.add_edge(i, j, cap, cap);
mf.add_edge(i, j, cap, cap);
mf2.add_edge(i, j, cap, cap);
Expand Down
6 changes: 3 additions & 3 deletions code/graph/edmonds_karps.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void test1() {

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int cap = rand() % MAX + 1;
int cap = rng() % MAX + 1;
mf.add_edge(i, j, cap);
mf2.add_edge(i, j, cap);
}
Expand All @@ -34,7 +34,7 @@ void test2() {

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int cap = rand() % MAX + 1;
int cap = rng() % MAX + 1;
mf.add_edge(i, j, cap, cap);
mf2.add_edge(i, j, cap, cap);
}
Expand All @@ -57,7 +57,7 @@ void test3() {

for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int cap = rand() % MAX + 1;
int cap = rng() % MAX + 1;
mf.add_edge(i, j, cap, cap);
mf.add_edge(i, j, cap, cap);
mf2.add_edge(i, j, cap, cap);
Expand Down
10 changes: 5 additions & 5 deletions code/graph/gomory_hu_tree.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void test() {
}
}

int cap = rand() % MAX;
int cap = rng() % MAX;
g.add_edge(i, j, cap, cap);
}
}
Expand All @@ -36,17 +36,17 @@ void test() {
// int cnt = 0;
// rep(i,0,N) {
// rep(j,0,N) {
// if (rand() % 1000 == 0) {
// g.add_edge(i,j,rand() % MAX);
// if (rng() % 1000 == 0) {
// g.add_edge(i,j,rng() % MAX);
// cnt++;
// }
// }
// }
//
// gh = construct_gh_tree(g);
// rep(i,0,N) {
// int a = rand() % N,
// b = rand() % N;
// int a = rng() % N,
// b = rng() % N;
// assert_equal(g.max_flow(a,b), compute_max_flow(a,b,gh));
// }
}
Expand Down
7 changes: 0 additions & 7 deletions code/graph/hld.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,9 @@ struct HLD_naive {
}
};

// int randint(int a, int b)
// {
// return rand() % (b - a + 1) + a;
// }

void test() {
/* Field testing: SPOJ LCA, SPOJ QTREE, TOJ 2241, Live Archive 2045, Live Archive 6140, UVa 10938, IPSC 2009 L */

// srand(100);

int ts = 100,
maxn = 1000,
maxq = 1000,
Expand Down
10 changes: 5 additions & 5 deletions code/graph/hopcroft_karp.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void test_rand(int N, int M, int sparseness = 2) {

for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(rand() % sparseness == 0) {
if(rng() % sparseness == 0) {
g.add_edge(i, j);
adj[i].push_back(j);
}
Expand All @@ -36,7 +36,7 @@ void test_rand(int N, int M, int sparseness = 2) {
void test() {
int TESTS = 1000, MAX_N = 400, MAX_M = 300;
for(int i = 0; i < TESTS; ++i) {
test_rand(rand() % MAX_N, rand() % MAX_M, rand() % 100 + 1);
test_rand(rng() % MAX_N, rng() % MAX_M, rng() % 100 + 1);
}
}

Expand All @@ -47,7 +47,7 @@ void test() {
//
// for(int i = 0; i < N; i++) {
// for(int j = 0; j < M; j++) {
// if(rand() % sparse == 0) {
// if(rng() % sparse == 0) {
// cnt++;
// g.add_edge(i, j);
// }
Expand All @@ -60,8 +60,8 @@ void test() {
// void test() {
// int TESTS = 20, MAX_N = 5000, MAX_M = 5000;
// for(int i = 0; i < TESTS; ++i) {
// // test_rand(rand() % MAX_N, rand() % MAX_M);
// test_rand(MAX_N, MAX_M, rand() % 100000);
// // test_rand(rng() % MAX_N, rng() % MAX_M);
// test_rand(MAX_N, MAX_M, rng() % 100000);
// }
// }
// vim: cc=60 ts=2 sts=2 sw=2:
14 changes: 7 additions & 7 deletions code/graph/tarjan_olca.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pair<vi*, int> random_tree(int n) {
union_find uf(n);

for (int i = 0; uf.size(0) < n; i++) {
int a = rand() % n,
b = rand() % n;
int a = rng() % n,
b = rng() % n;

if (uf.find(a) != uf.find(b)) {
uf.unite(a, b);
Expand All @@ -51,7 +51,7 @@ pair<vi*, int> random_tree(int n) {
}
}

int root = rand() % n;
int root = rng() % n;
stack<int> S;
S.push(root);
bool *visited = new bool[n];
Expand Down Expand Up @@ -133,8 +133,8 @@ void test() {
int tests = 100;

for (int t = 0; t < tests; t++) {
int n = rand() % 10000 + 1;
int q = rand() % 10000 + 1;
int n = rng() % 10000 + 1;
int q = rng() % 10000 + 1;

pair<vi*, int> xadj = random_tree(n);
vi *adj = xadj.first;
Expand All @@ -147,8 +147,8 @@ void test() {

vii qs;
for (int i = 0; i < q; i++) {
int a = rand() % n,
b = rand() % n;
int a = rng() % n,
b = rng() % n;

qs.push_back(ii(a, b));
lca.query(a, b);
Expand Down
3 changes: 3 additions & 0 deletions code/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ typedef vector<vi> vvi;
typedef vector<vii> vvii;
template <class T> T smod(T a, T b) {
return (a % b + b) % b; }

default_random_engine rng(
chrono::system_clock::now().time_since_epoch().count());
// vim: cc=60 ts=2 sts=2 sw=2:
Loading

0 comments on commit e8a38a5

Please sign in to comment.