misc/rng.hpp
- View this file on GitHub
- Last update: 2026-06-27 14:52:13+09:00
- Include:
#include "misc/rng.hpp"
Depends on
Required by
marathon/multi-armed-bandit.hpp
Multipoint Simulated Annealing
(marathon/sa-manager.hpp)
math/primitive-root-ll.hpp
math/two-square.hpp
Black Box Linear Algebra
(matrix/black-box-linear-algebra.hpp)
misc/all.hpp
kth root(Tonelli-Shanks algorithm)
(modulo/mod-kth-root.hpp)
多変数巡回畳み込み
(ntt/multivariate-circular-convolution.hpp)
ntt/ntt-64bit.hpp
高速素因数分解(Miller Rabin/Pollard's Rho)
(prime/fast-factorize.hpp)
遅延伝搬反転可能Treap
(rbst/treap.hpp)
Pruefer Code
(tree/pruefer-code.hpp)
Verified with
verify/verify-aoj-other/aoj-1068.test.cpp
verify/verify-aoj-other/aoj-2171-bigrational.test.cpp
verify/verify-unit-test/arbitrary-modint.test.cpp
verify/verify-unit-test/arbitrary-ntt-mod18446744069414584321.test.cpp
verify/verify-unit-test/barrett-reduction.test.cpp
verify/verify-unit-test/bigint-gcd.test.cpp
verify/verify-unit-test/bigint.test.cpp
verify/verify-unit-test/bigint2.test.cpp
verify/verify-unit-test/bigint3.test.cpp
verify/verify-unit-test/bigrational.test.cpp
verify/verify-unit-test/complex-fft.test.cpp
verify/verify-unit-test/composite-exp.test.cpp
verify/verify-unit-test/composition.test.cpp
verify/verify-unit-test/dijkstra.test.cpp
verify/verify-unit-test/dynamic-diameter-faster.test.cpp
verify/verify-unit-test/dynamic-diameter.test.cpp
verify/verify-unit-test/enumerate-convex.test.cpp
verify/verify-unit-test/enumerate-quotient.test.cpp
verify/verify-unit-test/factorize.test.cpp
verify/verify-unit-test/fast-bs.test.cpp
verify/verify-unit-test/fast-inv-o1.test.cpp
verify/verify-unit-test/fast-inv.test.cpp
verify/verify-unit-test/fft2d.test.cpp
verify/verify-unit-test/fps-sparse.test.cpp
verify/verify-unit-test/fps.test.cpp
verify/verify-unit-test/garner-bigint.test.cpp
verify/verify-unit-test/garner.test.cpp
verify/verify-unit-test/gauss-elimination.test.cpp
verify/verify-unit-test/internal-math.test.cpp
verify/verify-unit-test/internal-type-traits.test.cpp
verify/verify-unit-test/interval-union.test.cpp
verify/verify-unit-test/karatsuba.test.cpp
verify/verify-unit-test/lazyseg-bsearch.test.cpp
verify/verify-unit-test/lazyseg-setval-2.test.cpp
verify/verify-unit-test/lazyseg-setval.test.cpp
verify/verify-unit-test/li-chao-tree-abstract.test.cpp
verify/verify-unit-test/manacher.test.cpp
verify/verify-unit-test/math.test.cpp
verify/verify-unit-test/modint-2-61m1.test.cpp
verify/verify-unit-test/modint.test.cpp
verify/verify-unit-test/multieval.test.cpp
verify/verify-unit-test/multiplicative-function.test.cpp
verify/verify-unit-test/multipoint-binomial-sum.test.cpp
verify/verify-unit-test/nimber.test.cpp
verify/verify-unit-test/ntt-64bit.test.cpp
verify/verify-unit-test/orderedmap.test.cpp
verify/verify-unit-test/osak.test.cpp
verify/verify-unit-test/parallel-union-find.test.cpp
verify/verify-unit-test/partial-fraction-decomposition.test.cpp
verify/verify-unit-test/polynomial-matrix-prod.test.cpp
verify/verify-unit-test/primality-test.test.cpp
verify/verify-unit-test/primitive-root.test.cpp
verify/verify-unit-test/radix-heap.test.cpp
verify/verify-unit-test/radix-sort.test.cpp
verify/verify-unit-test/rational-number.test.cpp
verify/verify-unit-test/rbst-segment-tree.test.cpp
verify/verify-unit-test/rbst-sequence.test.cpp
verify/verify-unit-test/relaxed-convolution.test.cpp
verify/verify-unit-test/rerooting.test.cpp
verify/verify-unit-test/run-length-encoding.test.cpp
verify/verify-unit-test/sa-manager.test.cpp
verify/verify-unit-test/segment-set.test.cpp
verify/verify-unit-test/segment-tree-beats.test.cpp
verify/verify-unit-test/semiring.test.cpp
verify/verify-unit-test/set-function.test.cpp
verify/verify-unit-test/simulated-annealing.test.cpp
verify/verify-unit-test/sparse-table.test.cpp
verify/verify-unit-test/strassen.test.cpp
verify/verify-unit-test/string-search.test.cpp
verify/verify-unit-test/template.test.cpp
verify/verify-unit-test/tree-path.test.cpp
verify/verify-yosupo-ds/yosupo-dynamic-sequence-range-affine-range-sum-treap.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-add-path-sum-euler-tour.test.cpp
verify/verify-yosupo-math/yosupo-concave-min-plus-convolution-1.test.cpp
verify/verify-yosupo-math/yosupo-concave-min-plus-convolution-2.test.cpp
verify/verify-yosupo-math/yosupo-determinant-of-matrix-bbla.test.cpp
verify/verify-yosupo-math/yosupo-determinant-of-sparse-matrix-bbla.test.cpp
verify/verify-yosupo-math/yosupo-factorization.test.cpp
verify/verify-yosupo-math/yosupo-kth-root-mod.test.cpp
verify/verify-yosupo-math/yosupo-primitive-root.test.cpp
verify/verify-yosupo-math/yosupo-two-square-sum.test.cpp
verify/verify-yosupo-ntt/yosupo-multipoint-evaluation-chirp-z.test.cpp
verify/verify-yosupo-ntt/yosupo-multivariate-circular-convolution.test.cpp
verify/verify-yuki/yuki-0002.test.cpp
verify/verify-yuki/yuki-0103.test.cpp
verify/verify-yuki/yuki-0952.test.cpp
verify/verify-yuki/yuki-1112-sparse.test.cpp
verify/verify-yuki/yuki-1112.test.cpp
verify/verify-yuki/yuki-1775.test.cpp
Code
#pragma once
#include <algorithm>
#include <cassert>
#include <unordered_set>
#include <vector>
using namespace std;
#include "../internal/internal-seed.hpp"
namespace my_rand {
using i64 = long long;
using u64 = unsigned long long;
// [0, 2^64 - 1)
u64 rng() {
static u64 _x = nyaan_internal::seed();
return _x ^= _x << 7, _x ^= _x >> 9;
}
// [l, r]
i64 rng(i64 l, i64 r) {
assert(l <= r);
return l + rng() % u64(r - l + 1);
}
// [l, r)
i64 randint(i64 l, i64 r) {
assert(l < r);
return l + rng() % u64(r - l);
}
// choose n numbers from [l, r) without overlapping
vector<i64> randset(i64 l, i64 r, i64 n) {
assert(l <= r && n <= r - l);
unordered_set<i64> s;
for (i64 i = n; i; --i) {
i64 m = randint(l, r + 1 - i);
if (s.find(m) != s.end()) m = r - i;
s.insert(m);
}
vector<i64> ret;
for (auto& x : s) ret.push_back(x);
sort(begin(ret), end(ret));
return ret;
}
// [0.0, 1.0)
double rnd() { return rng() * 5.42101086242752217004e-20; }
// [l, r)
double rnd(double l, double r) {
assert(l < r);
return l + rnd() * (r - l);
}
template <typename T>
void randshf(vector<T>& v) {
int n = v.size();
for (int i = 1; i < n; i++) swap(v[i], v[randint(0, i + 1)]);
}
} // namespace my_rand
using my_rand::randint;
using my_rand::randset;
using my_rand::randshf;
using my_rand::rnd;
using my_rand::rng;#line 2 "misc/rng.hpp"
#include <algorithm>
#include <cassert>
#include <unordered_set>
#include <vector>
using namespace std;
#line 2 "internal/internal-seed.hpp"
#include <chrono>
using namespace std;
namespace nyaan_internal {
unsigned long long non_deterministic_seed() {
unsigned long long m =
chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count();
m ^= 9845834732710364265uLL;
m ^= m << 24, m ^= m >> 31, m ^= m << 35;
return m;
}
unsigned long long deterministic_seed() { return 88172645463325252UL; }
// 64 bit の seed 値を生成 (手元では seed 固定)
// 連続で呼び出すと同じ値が何度も返ってくるので注意
// #define RANDOMIZED_SEED するとシードがランダムになる
unsigned long long seed() {
#if defined(NyaanLocal) && !defined(RANDOMIZED_SEED)
return deterministic_seed();
#else
return non_deterministic_seed();
#endif
}
} // namespace nyaan_internal
#line 10 "misc/rng.hpp"
namespace my_rand {
using i64 = long long;
using u64 = unsigned long long;
// [0, 2^64 - 1)
u64 rng() {
static u64 _x = nyaan_internal::seed();
return _x ^= _x << 7, _x ^= _x >> 9;
}
// [l, r]
i64 rng(i64 l, i64 r) {
assert(l <= r);
return l + rng() % u64(r - l + 1);
}
// [l, r)
i64 randint(i64 l, i64 r) {
assert(l < r);
return l + rng() % u64(r - l);
}
// choose n numbers from [l, r) without overlapping
vector<i64> randset(i64 l, i64 r, i64 n) {
assert(l <= r && n <= r - l);
unordered_set<i64> s;
for (i64 i = n; i; --i) {
i64 m = randint(l, r + 1 - i);
if (s.find(m) != s.end()) m = r - i;
s.insert(m);
}
vector<i64> ret;
for (auto& x : s) ret.push_back(x);
sort(begin(ret), end(ret));
return ret;
}
// [0.0, 1.0)
double rnd() { return rng() * 5.42101086242752217004e-20; }
// [l, r)
double rnd(double l, double r) {
assert(l < r);
return l + rnd() * (r - l);
}
template <typename T>
void randshf(vector<T>& v) {
int n = v.size();
for (int i = 1; i < n; i++) swap(v[i], v[randint(0, i + 1)]);
}
} // namespace my_rand
using my_rand::randint;
using my_rand::randset;
using my_rand::randshf;
using my_rand::rnd;
using my_rand::rng;