rcpl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: random/test/base.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>

#include "../base.hpp"

void test1_same_seed() {
    for (int seed = 0; seed < 10; seed++) {
        RandomFixed rng_a(seed);
        RandomFixed rng_b(seed);
        std::vector<uint64_t> a(10), b(10);
        for (int i = 0; i < 10; i++) {
            a[i] = rng_a.rand_int();
            b[i] = rng_b.rand_int();
        }
        assert(a == b);
    }
}

void test2_different_seed() {
    for (int seed = 0; seed < 10; seed++) {
        RandomFixed rng_a(seed);
        RandomFixed rng_b(seed + 1);
        std::vector<uint64_t> a(10), b(10);
        for (int i = 0; i < 10; i++) {
            a[i] = rng_a.rand_int();
            b[i] = rng_b.rand_int();
        }
        assert(a != b);
    }
}

void test3_auto_seed() {
    RandomAuto rng_a;
    RandomFixed rng_b(0);
    std::vector<uint64_t> a(10), b(10);
    for (int i = 0; i < 10; i++) {
        a[i] = rng_a.rand_int();
        b[i] = rng_b.rand_int();
    }
    assert(a != b);
}

void test4_negative() {
    RandomAuto rng;
    auto v = rng.rand_int(-100, -1);
    assert(-100 <= v and v <= -1);
}

void test5_big() {
    RandomAuto rng;
    const int BIG_INT = 1000000000;
    auto v = rng.rand_int(-BIG_INT, BIG_INT);
    assert(-BIG_INT <= v and v <= BIG_INT);
}

void test6_double() {
    RandomAuto rng;
    auto v = rng.rand_double();
    assert(0.0 <= v and v <= 1.0);
    auto v2 = rng.rand_double(-100.0, -1.0);
    assert(-100.0 <= v2 and v2 <= -1.0);
}

int main() {
    test1_same_seed();
    test2_different_seed();
    test3_auto_seed();
    test4_negative();
    test5_big();
    test6_double();
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}
#line 1 "random/test/base.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>

#line 2 "random/base.hpp"

#line 4 "random/base.hpp"
#include <chrono>
#line 6 "random/base.hpp"

#line 2 "misc/topbit.hpp"

#line 2 "misc/countl_zero.hpp"

#if __cplusplus >= 202002L
#include <bit>
#endif

// countl_zero
// (000, 001, 010, 011, 100) -> (32, 31, 30, 30, 29)
#if __cplusplus >= 202002L
using std::countl_zero;
#else
int countl_zero(unsigned int x) {
    return x == 0 ? 32 : __builtin_clz(x);
}
int countl_zero(unsigned long long int x) {
    return x == 0 ? 64 : __builtin_clzll(x);
}
#endif
int countl_zero(int x) { return countl_zero((unsigned int)(x)); }
int countl_zero(long long int x) {
    return countl_zero((unsigned long long int)(x));
}
#line 4 "misc/topbit.hpp"

// topbit
// (000, 001, 010, 011, 100) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return 31 - countl_zero(x); }
int topbit(unsigned int x) { return 31 - countl_zero(x); }
int topbit(long long int x) { return 63 - countl_zero(x); }
int topbit(unsigned long long int x) { return 63 - countl_zero(x); }
#line 8 "random/base.hpp"

template <bool auto_seed> struct Random {
    uint64_t x_seed;

    Random(uint64_t seed = 0) {
        if (auto_seed) {
            // set random seed
            assert(seed == 0);
            x_seed =
                std::chrono::steady_clock::now().time_since_epoch().count();
        } else {
            // set seed
            x_seed = seed;
        }
    }

    // http://xorshift.di.unimi.it/splitmix64.c
    // [0, 2^64 - 1]
    uint64_t rand_int() {
        uint64_t z = (x_seed += 0x9e3779b97f4a7c15);
        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
        z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
        return z ^ (z >> 31);
    }

    // [0, mod - 1]
    // rand_int() % mod だと mod が 2 べきでないときに偏る
    uint64_t rand_int(uint64_t mod) {
        assert(mod > 0);
        if ((mod & (mod - 1)) == 0) {
            // mod = 2^p
            // (mod - 1) = 0...01...1
            return rand_int() & (mod - 1);
        }
        // mod >= 3 (1 = 2^0, 2 = 2^1)
        int lg = topbit((unsigned long long)mod);
        uint64_t mask = (lg == 63) ? ~0ULL : (1ULL << (lg + 1)) - 1;
        while (true) {
            uint64_t r = rand_int() & mask;
            if (r < mod) return r;
        }
    }

    // [l, r]
    template <class T> T rand_int(T l, T r) {
        assert(l <= r);
        return T(l + rand_int(uint64_t(r - l + 1)));
    }

    // [0.0, 1.0]
    double rand_double() {
        uint64_t v = rand_int(1ULL << 63);
        return double(v) / ((1ULL << 63) - 1);
    }

    // [l, r]
    double rand_double(double l, double r) {
        assert(l <= r);
        return l + rand_double() * (r - l);
    }
};

using RandomFixed = Random<false>;
using RandomAuto = Random<true>;

RandomAuto rng_auto;
#line 9 "random/test/base.test.cpp"

void test1_same_seed() {
    for (int seed = 0; seed < 10; seed++) {
        RandomFixed rng_a(seed);
        RandomFixed rng_b(seed);
        std::vector<uint64_t> a(10), b(10);
        for (int i = 0; i < 10; i++) {
            a[i] = rng_a.rand_int();
            b[i] = rng_b.rand_int();
        }
        assert(a == b);
    }
}

void test2_different_seed() {
    for (int seed = 0; seed < 10; seed++) {
        RandomFixed rng_a(seed);
        RandomFixed rng_b(seed + 1);
        std::vector<uint64_t> a(10), b(10);
        for (int i = 0; i < 10; i++) {
            a[i] = rng_a.rand_int();
            b[i] = rng_b.rand_int();
        }
        assert(a != b);
    }
}

void test3_auto_seed() {
    RandomAuto rng_a;
    RandomFixed rng_b(0);
    std::vector<uint64_t> a(10), b(10);
    for (int i = 0; i < 10; i++) {
        a[i] = rng_a.rand_int();
        b[i] = rng_b.rand_int();
    }
    assert(a != b);
}

void test4_negative() {
    RandomAuto rng;
    auto v = rng.rand_int(-100, -1);
    assert(-100 <= v and v <= -1);
}

void test5_big() {
    RandomAuto rng;
    const int BIG_INT = 1000000000;
    auto v = rng.rand_int(-BIG_INT, BIG_INT);
    assert(-BIG_INT <= v and v <= BIG_INT);
}

void test6_double() {
    RandomAuto rng;
    auto v = rng.rand_double();
    assert(0.0 <= v and v <= 1.0);
    auto v2 = rng.rand_double(-100.0, -1.0);
    assert(-100.0 <= v2 and v2 <= -1.0);
}

int main() {
    test1_same_seed();
    test2_different_seed();
    test3_auto_seed();
    test4_negative();
    test5_big();
    test6_double();
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}
Back to top page