请注意random库与chrono库均是C++11。
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 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include <chrono> #include <iostream> #include <random>
using namespace std; typedef long long LL; typedef unsigned long long ULL;
int rand(int a, int b) { static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<int>(a, b)(rng); }
unsigned rand(unsigned a, unsigned b) { static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<unsigned>(a, b)(rng); }
LL rand(LL a, LL b) { static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<LL>(a, b)(rng); }
ULL rand(ULL a, ULL b) { static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<ULL>(a, b)(rng); }
int main() { const LL MAX = 1e10; cout << rand(-MAX, MAX); return 0; }
|