From e8a38a5f03cdac6b5ae6cebd6d2b9d42499fd609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjarki=20=C3=81g=C3=BAst=20Gu=C3=B0mundsson?= Date: Mon, 30 Dec 2024 13:57:47 +0000 Subject: [PATCH] Use standard random library Addresses compiler warnings and use of deprecated functions. --- code/data-structures/avl_tree.test.cpp | 12 ++++++------ code/data-structures/kd_tree.test.cpp | 8 ++++---- code/data-structures/misof_tree.test.cpp | 8 ++++---- code/data-structures/sparse_table.test.cpp | 2 +- code/geometry/welzl.cpp | 2 +- code/graph/bipartite_matching.test.cpp | 4 ++-- code/graph/bipartite_mvc.test.cpp | 8 ++++---- code/graph/blossom.cpp | 2 +- code/graph/dinic.test.cpp | 6 +++--- code/graph/edmonds_karps.test.cpp | 6 +++--- code/graph/gomory_hu_tree.test.cpp | 10 +++++----- code/graph/hld.test.cpp | 7 ------- code/graph/hopcroft_karp.test.cpp | 10 +++++----- code/graph/tarjan_olca.test.cpp | 14 +++++++------- code/header.cpp | 3 +++ code/mathematics/berlekamp_massey.test.cpp | 20 ++++++++++---------- code/mathematics/fastmul.test.cpp | 10 ++++------ code/mathematics/fft.test.cpp | 8 ++++---- code/mathematics/fht.test.cpp | 6 +++--- code/mathematics/intx.test.cpp | 4 ++-- code/mathematics/linear_recurrence.test.cpp | 14 +++++++------- code/mathematics/miller_rabin.cpp | 2 +- code/mathematics/miller_rabin.test.cpp | 2 +- code/mathematics/mod_inv_sieve.test.cpp | 2 +- code/mathematics/tridiagonal.test.cpp | 2 +- code/other/dpll.test.cpp | 8 ++++---- code/other/lis.test.cpp | 2 +- code/other/simulated_annealing.cpp | 3 +-- code/other/stable_marriage.test.cpp | 6 +++--- code/strings/aho_corasick.test.cpp | 8 ++++---- code/strings/kmp.test.cpp | 8 ++++---- code/strings/suffix_array.test.cpp | 4 ++-- code/tricks/is_square.test.cpp | 2 +- tester/test_footer.cpp | 1 - tester/test_header.cpp | 4 ++-- 35 files changed, 105 insertions(+), 113 deletions(-) diff --git a/code/data-structures/avl_tree.test.cpp b/code/data-structures/avl_tree.test.cpp index c125023..e5b7831 100644 --- a/code/data-structures/avl_tree.test.cpp +++ b/code/data-structures/avl_tree.test.cpp @@ -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::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::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)); @@ -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::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::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)); diff --git a/code/data-structures/kd_tree.test.cpp b/code/data-structures/kd_tree.test.cpp index b596e94..d00a457 100644 --- a/code/data-structures/kd_tree.test.cpp +++ b/code/data-structures/kd_tree.test.cpp @@ -66,7 +66,7 @@ void randomize_pt() { hi = 10; for (int i = 0; i < CURK; i++) { - pt[i] = static_cast(rand()) / RAND_MAX * (hi - lo) + lo; + pt[i] = uniform_real_distribution(lo, hi)(rng); pt[i] = round(pt[i] * 3.0) / 3.0; } } @@ -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::pt> pts1(startcnt); vector::point> pts2(startcnt); @@ -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 @@ -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::pt, bool> a = tree1.nearest_neighbour(kd_tree::pt(pt), allow_same); diff --git a/code/data-structures/misof_tree.test.cpp b/code/data-structures/misof_tree.test.cpp index 3b7e78b..3000682 100644 --- a/code/data-structures/misof_tree.test.cpp +++ b/code/data-structures/misof_tree.test.cpp @@ -5,10 +5,10 @@ void test() { avl_tree 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); @@ -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)); diff --git a/code/data-structures/sparse_table.test.cpp b/code/data-structures/sparse_table.test.cpp index 9b27975..fd42e18 100644 --- a/code/data-structures/sparse_table.test.cpp +++ b/code/data-structures/sparse_table.test.cpp @@ -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); diff --git a/code/geometry/welzl.cpp b/code/geometry/welzl.cpp index 5feab81..72ee533 100644 --- a/code/geometry/welzl.cpp +++ b/code/geometry/welzl.cpp @@ -13,7 +13,7 @@ pair 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 D = welzl(); if (abs(res - D.first) > D.second + EPS) { diff --git a/code/graph/bipartite_matching.test.cpp b/code/graph/bipartite_matching.test.cpp index 30f5338..00f5002 100644 --- a/code/graph/bipartite_matching.test.cpp +++ b/code/graph/bipartite_matching.test.cpp @@ -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); @@ -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: diff --git a/code/graph/bipartite_mvc.test.cpp b/code/graph/bipartite_mvc.test.cpp index 4713bb9..9f8efea 100644 --- a/code/graph/bipartite_mvc.test.cpp +++ b/code/graph/bipartite_mvc.test.cpp @@ -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)); } } diff --git a/code/graph/blossom.cpp b/code/graph/blossom.cpp index 25f3d91..5e12d48 100644 --- a/code/graph/blossom.cpp +++ b/code/graph/blossom.cpp @@ -73,7 +73,7 @@ vi find_augmenting_path(const vector &adj,const vi &m){ vii max_matching(const vector &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); diff --git a/code/graph/dinic.test.cpp b/code/graph/dinic.test.cpp index d2b9ce8..b17b185 100644 --- a/code/graph/dinic.test.cpp +++ b/code/graph/dinic.test.cpp @@ -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); } @@ -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); } @@ -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); diff --git a/code/graph/edmonds_karps.test.cpp b/code/graph/edmonds_karps.test.cpp index 2146060..a5acc16 100644 --- a/code/graph/edmonds_karps.test.cpp +++ b/code/graph/edmonds_karps.test.cpp @@ -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); } @@ -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); } @@ -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); diff --git a/code/graph/gomory_hu_tree.test.cpp b/code/graph/gomory_hu_tree.test.cpp index fad4df0..5db7943 100644 --- a/code/graph/gomory_hu_tree.test.cpp +++ b/code/graph/gomory_hu_tree.test.cpp @@ -16,7 +16,7 @@ void test() { } } - int cap = rand() % MAX; + int cap = rng() % MAX; g.add_edge(i, j, cap, cap); } } @@ -36,8 +36,8 @@ 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++; // } // } @@ -45,8 +45,8 @@ void test() { // // 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)); // } } diff --git a/code/graph/hld.test.cpp b/code/graph/hld.test.cpp index 7695f6d..8ae629b 100644 --- a/code/graph/hld.test.cpp +++ b/code/graph/hld.test.cpp @@ -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, diff --git a/code/graph/hopcroft_karp.test.cpp b/code/graph/hopcroft_karp.test.cpp index 3ba47c8..c2a382c 100644 --- a/code/graph/hopcroft_karp.test.cpp +++ b/code/graph/hopcroft_karp.test.cpp @@ -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); } @@ -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); } } @@ -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); // } @@ -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: diff --git a/code/graph/tarjan_olca.test.cpp b/code/graph/tarjan_olca.test.cpp index 2c101dc..0c02fb6 100644 --- a/code/graph/tarjan_olca.test.cpp +++ b/code/graph/tarjan_olca.test.cpp @@ -41,8 +41,8 @@ pair 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); @@ -51,7 +51,7 @@ pair random_tree(int n) { } } - int root = rand() % n; + int root = rng() % n; stack S; S.push(root); bool *visited = new bool[n]; @@ -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 xadj = random_tree(n); vi *adj = xadj.first; @@ -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); diff --git a/code/header.cpp b/code/header.cpp index 1963cc6..df03a12 100644 --- a/code/header.cpp +++ b/code/header.cpp @@ -16,4 +16,7 @@ typedef vector vvi; typedef vector vvii; template 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: diff --git a/code/mathematics/berlekamp_massey.test.cpp b/code/mathematics/berlekamp_massey.test.cpp index 440ead8..5a59b0c 100644 --- a/code/mathematics/berlekamp_massey.test.cpp +++ b/code/mathematics/berlekamp_massey.test.cpp @@ -31,15 +31,15 @@ ostream& operator <<(ostream &outs, const Mod &x) { void testMod() { for (int it = 0; it < 10000; it++) { - int K = rand() % 20 + 1, - S = rand() % 1000 + 1; + int K = rng() % 20 + 1, + S = rng() % 1000 + 1; - int k = rand() % K + 1; + int k = rng() % K + 1; vector init(k); - rep(i,0,k) init[i] = rand() % (2*S) - S; + rep(i,0,k) init[i] = (int)(rng() % (2*S)) - S; vector c(k); - rep(i,0,k) c[i] = rand() % (2*S) - S; + rep(i,0,k) c[i] = (int)(rng() % (2*S)) - S; rep(i,0,k) { Mod cur = 0; rep(j,0,k) { @@ -62,15 +62,15 @@ void testMod() { void testDouble() { for (int it = 0; it < 10000; it++) { - int K = rand() % 5 + 1, - S = rand() % 10 + 1; + int K = rng() % 5 + 1, + S = rng() % 10 + 1; - int k = rand() % K + 1; + int k = rng() % K + 1; vector init(k); - rep(i,0,k) init[i] = rand() % (2*S) - S; + rep(i,0,k) init[i] = (int)(rng() % (2*S)) - S; vector c(k); - rep(i,0,k) c[i] = rand() % (2*S) - S; + rep(i,0,k) c[i] = (int)(rng() % (2*S)) - S; rep(i,0,k) { long double cur = 0; rep(j,0,k) { diff --git a/code/mathematics/fastmul.test.cpp b/code/mathematics/fastmul.test.cpp index 5ac2d4b..ad01571 100644 --- a/code/mathematics/fastmul.test.cpp +++ b/code/mathematics/fastmul.test.cpp @@ -1,9 +1,9 @@ intx randint(int len) { stringstream ss; - ss << rand() % 9 + 1; + ss << rng() % 9 + 1; for (int i = 0; i < len-1; i++) - ss << rand() % 10; + ss << rng() % 10; return intx(ss.str()); } @@ -11,8 +11,6 @@ intx randint(int len) { void test() { /* Field testing: SPOJ {MUL,TMUL,VFMUL} */ - srand(1337); // TODO: remove this - int ts = 1000, l1 = 1, l2 = 1000; @@ -20,8 +18,8 @@ void test() { for (int t = 0; t < ts; t++) { // printf("%d\n", t); - int la = rand() % (l2 - l1 + 1) + l1, - lb = rand() % (l2 - l1 + 1) + l1; + int la = uniform_int_distribution(l1, l2)(rng), + lb = uniform_int_distribution(l1, l2)(rng); intx a = randint(la), b = randint(lb), diff --git a/code/mathematics/fft.test.cpp b/code/mathematics/fft.test.cpp index 6c45d41..7b5e20b 100644 --- a/code/mathematics/fft.test.cpp +++ b/code/mathematics/fft.test.cpp @@ -86,9 +86,9 @@ intx fastmul(intx an, intx bn) intx randint(int len) { stringstream ss; - ss << rand() % 9 + 1; + ss << rng() % 9 + 1; for (int i = 0; i < len-1; i++) - ss << rand() % 10; + ss << rng() % 10; return intx(ss.str()); } @@ -102,8 +102,8 @@ void test() { for (int t = 0; t < ts; t++) { - int la = rand() % (l2 - l1 + 1) + l1, - lb = rand() % (l2 - l1 + 1) + l1; + int la = uniform_int_distribution(l1, l2)(rng), + lb = uniform_int_distribution(l1, l2)(rng); intx a = randint(la), b = randint(lb), diff --git a/code/mathematics/fht.test.cpp b/code/mathematics/fht.test.cpp index fab77a3..a111b60 100644 --- a/code/mathematics/fht.test.cpp +++ b/code/mathematics/fht.test.cpp @@ -12,12 +12,12 @@ vi slow(vi a, vi b) { void test() { rep(it,0,1000) { - int k = rand() % 10; + int k = rng() % 10; int n = 1< init, c; - rep(i,0,k) init.push_back(rand() % mod); - rep(i,0,k) c.push_back(rand() % mod); + rep(i,0,k) init.push_back(rng() % mod); + rep(i,0,k) c.push_back(rng() % mod); for (int jt = 0; jt < 20; jt++) { - ll n = rand() % 10; + ll n = rng() % 10; assert_equal(slow(init, c, n, mod), nth_term(init, c, n, mod), true); - n = rand() % 100; + n = rng() % 100; assert_equal(slow(init, c, n, mod), nth_term(init, c, n, mod), true); - n = rand() % 1000; + n = rng() % 1000; assert_equal(slow(init, c, n, mod), nth_term(init, c, n, mod), true); - n = rand() % 1000; + n = rng() % 1000; assert_equal(slow(init, c, n, 123456), nth_term(init, c, n, 123456), true); } } diff --git a/code/mathematics/miller_rabin.cpp b/code/mathematics/miller_rabin.cpp index f0959f4..6f3801d 100644 --- a/code/mathematics/miller_rabin.cpp +++ b/code/mathematics/miller_rabin.cpp @@ -5,7 +5,7 @@ bool is_probable_prime(ll n, int k) { int s = 0; ll d = n - 1; while (~d & 1) d >>= 1, s++; while (k--) { - ll a = (n - 3) * rand() / RAND_MAX + 2; + ll a = uniform_int_distribution(2LL, n-1)(rng); ll x = mod_pow(a, d, n); if (x == 1 || x == n - 1) continue; bool ok = false; diff --git a/code/mathematics/miller_rabin.test.cpp b/code/mathematics/miller_rabin.test.cpp index 540df41..16d49d4 100644 --- a/code/mathematics/miller_rabin.test.cpp +++ b/code/mathematics/miller_rabin.test.cpp @@ -9,7 +9,7 @@ void test() { } for (int i = 0; i < 10000; i++) { - int x = rand() % 100000000; + int x = rng() % 100000000; bool a = is_probable_prime(x, 10), b = is_prime(x); diff --git a/code/mathematics/mod_inv_sieve.test.cpp b/code/mathematics/mod_inv_sieve.test.cpp index a5b0912..5f17a27 100644 --- a/code/mathematics/mod_inv_sieve.test.cpp +++ b/code/mathematics/mod_inv_sieve.test.cpp @@ -1,7 +1,7 @@ #include "../code/mathematics/is_prime.cpp" void test() { rep(it,0,300) { - int p = rand() % 2000000000; + int p = rng() % 2000000000; while (!is_prime(p)) p++; int n = min(100000, p); vi inv = inv_sieve(n, p); diff --git a/code/mathematics/tridiagonal.test.cpp b/code/mathematics/tridiagonal.test.cpp index f2c7b0c..ebb9ea0 100644 --- a/code/mathematics/tridiagonal.test.cpp +++ b/code/mathematics/tridiagonal.test.cpp @@ -2,7 +2,7 @@ void test() { long double eps = 1e-5; rep(it,0,5000) { - int n = rand() % min(it+1, 4999) + 1; + int n = rng() % min(it+1, 4999) + 1; vector cX(n); rep(i,0,n) { A[i] = randdouble(-1000, 1000); diff --git a/code/other/dpll.test.cpp b/code/other/dpll.test.cpp index d7e1fd7..fee847f 100644 --- a/code/other/dpll.test.cpp +++ b/code/other/dpll.test.cpp @@ -2,14 +2,14 @@ void test() { // Field testing: https://icpc.kattis.com/problems/checks // Was too slow on https://open.kattis.com/problems/boardcovering rep(its,0,1000) { - int n = rand() % 20 + 1, - m = rand() % (4*n); + int n = rng() % 20 + 1, + m = rng() % (4*n); vector cl; rep(i,0,m) { vi cur; while (cur.empty()) rep(j,0,n) { - if (rand() % 4 == 0) { - if (rand() % 2 == 0) { + if (rng() % 4 == 0) { + if (rng() % 2 == 0) { cur.push_back(-(j+1)); } else { cur.push_back(j+1); diff --git a/code/other/lis.test.cpp b/code/other/lis.test.cpp index de38395..bd6d220 100644 --- a/code/other/lis.test.cpp +++ b/code/other/lis.test.cpp @@ -15,7 +15,7 @@ int lis_dp(const vi &arr, int last) { void check(int n) { vi arr(n); - rep(i,0,n) arr[i] = rand() % 100; + rep(i,0,n) arr[i] = rng() % 100; mem.clear(); int mx = 0; diff --git a/code/other/simulated_annealing.cpp b/code/other/simulated_annealing.cpp index 1f16347..f24d2ad 100644 --- a/code/other/simulated_annealing.cpp +++ b/code/other/simulated_annealing.cpp @@ -1,13 +1,12 @@ double curtime() { return static_cast(clock()) / CLOCKS_PER_SEC; } int simulated_annealing(int n, double seconds) { - default_random_engine rng; uniform_real_distribution randfloat(0.0, 1.0); uniform_int_distribution randint(0, n - 2); // random initial solution vi sol(n); rep(i,0,n) sol[i] = i + 1; - random_shuffle(sol.begin(), sol.end()); + shuffle(sol.begin(), sol.end(), rng); // initialize score int score = 0; rep(i,1,n) score += abs(sol[i] - sol[i-1]); diff --git a/code/other/stable_marriage.test.cpp b/code/other/stable_marriage.test.cpp index 9672843..dcb2e21 100644 --- a/code/other/stable_marriage.test.cpp +++ b/code/other/stable_marriage.test.cpp @@ -19,15 +19,15 @@ void test() { for (int t = 0; t < ts; t++) { - int n = rand() % maxn; + int n = rng() % maxn; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { m[i][j] = w[i][j] = j; } - random_shuffle(m[i], m[i] + n); - random_shuffle(w[i], w[i] + n); + shuffle(m[i], m[i] + n, rng); + shuffle(w[i], w[i] + n, rng); for (int j = 0; j < n; j++) { minv[i][m[i][j]] = j; diff --git a/code/strings/aho_corasick.test.cpp b/code/strings/aho_corasick.test.cpp index 026bb1a..8bd3eac 100644 --- a/code/strings/aho_corasick.test.cpp +++ b/code/strings/aho_corasick.test.cpp @@ -31,7 +31,7 @@ struct aho_corasick_slow { string random_string(int n, int cc) { stringstream ss; for (int i = 0; i < n; i++) { - ss << static_cast(rand() % cc + 'a'); + ss << static_cast(rng() % cc + 'a'); } return ss.str(); @@ -44,18 +44,18 @@ void test() { ts2 = 10; for (int t = 0; t < ts; t++) { - int n = rand() % 1000; + int n = rng() % 1000; vector kws; for (int i = 0; i < n; i++) { - kws.push_back(random_string(rand() % 10 + 1, rand() % 5 + 1)); + kws.push_back(random_string(rng() % 10 + 1, rng() % 5 + 1)); } aho_corasick ac(kws); aho_corasick_slow ac2(kws); for (int p = 0; p < ts2; p++) { - string s = random_string(rand() % 100, rand() % 5 + 1); + string s = random_string(rng() % 100, rng() % 5 + 1); vector res = ac.search(s); vector res2 = ac2.search(s); sort(res.begin(), res.end()); diff --git a/code/strings/kmp.test.cpp b/code/strings/kmp.test.cpp index 246e3d3..5472027 100644 --- a/code/strings/kmp.test.cpp +++ b/code/strings/kmp.test.cpp @@ -1,11 +1,11 @@ string rand_string(int l, int h) { - int len = rand() % (h - l + 1) + l; + int len = uniform_int_distribution(l, h)(rng); stringstream ss; rep(i,0,len) { - if (rand() % 2 == 0) { - ss << static_cast(rand() % 26 + 'a'); + if (rng() % 2 == 0) { + ss << static_cast(rng() % 26 + 'a'); } else { - ss << static_cast(rand() % 26 + 'A'); + ss << static_cast(rng() % 26 + 'A'); } } return ss.str(); diff --git a/code/strings/suffix_array.test.cpp b/code/strings/suffix_array.test.cpp index a24fecd..9952b57 100644 --- a/code/strings/suffix_array.test.cpp +++ b/code/strings/suffix_array.test.cpp @@ -88,10 +88,10 @@ void test() { make_sure_ok("ABAAAAAAA"); for (int i = 0; i < 10000; i++) { - int len = rand() % 1000; + int len = rng() % 1000; stringstream ss; for (int j = 0; j < len; j++) { - ss << static_cast('A' + (rand() % 26)); + ss << static_cast('A' + (rng() % 26)); } string s = ss.str(); diff --git a/code/tricks/is_square.test.cpp b/code/tricks/is_square.test.cpp index ceb0a3b..2964397 100644 --- a/code/tricks/is_square.test.cpp +++ b/code/tricks/is_square.test.cpp @@ -8,7 +8,7 @@ void test() { assert_true(is_square(4)); rep(i,0,100000) { - int n = rand() % 100000000; + int n = rng() % 100000000; int st = sqrt(n); while (st*st > n) st--; while (st*st < n) st++; diff --git a/tester/test_footer.cpp b/tester/test_footer.cpp index 65dcb61..3fc6117 100644 --- a/tester/test_footer.cpp +++ b/tester/test_footer.cpp @@ -1,5 +1,4 @@ int main() { - srand(time(NULL)); test(); assert(!ANY_FAILED); return 0; diff --git a/tester/test_header.cpp b/tester/test_header.cpp index 93cb435..6e4d7ca 100644 --- a/tester/test_header.cpp +++ b/tester/test_header.cpp @@ -48,9 +48,9 @@ void assert_almost_equal(double a, double b, double eps = 1e-9, bool kill = fals } double randdouble(double a, double b) { - return static_cast(rand()) / RAND_MAX * (b - a) + a; + return uniform_real_distribution(a, b)(rng); } int randint(int a, int b) { - return static_cast(randdouble(a, b) + 0.5); + return uniform_int_distribution(a, b)(rng); }