-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathxorshift.cpp
More file actions
55 lines (48 loc) · 1.4 KB
/
Copy pathxorshift.cpp
File metadata and controls
55 lines (48 loc) · 1.4 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// XorShift
//
// cf.
// ビット演算 (bit 演算) の使い方を総特集! 〜 マスクビットから bit DP まで 〜
// https://qiita.com/drken/items/7c6ff2aa4d8fce1c9361
//
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
// xor128による乱数生成、周期は2^128-1
unsigned int rand_int() {
static unsigned int tx = 123456789, ty=362436069, tz=521288629, tw=88675123;
unsigned int tt = (tx^(tx<<11));
tx = ty; ty = tz; tz = tw;
return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) );
}
int rand_int(int minv, int maxv) {
return rand_int() % (maxv - minv + 1) + minv;
}
long long rand_ll(long long minv, long long maxv) {
long long a = rand_int(), b = rand_int();
return (a * (1LL<<29) + b) % (maxv - minv + 1) + minv;
}
template<class T> void shuffle(vector<T>& vec) {
int n = vec.size();
for (int i = n - 1; i > 0; --i) {
int k = rand_int() % (i + 1);
swap(vec[i], vec[k]);
}
}
//------------------------------//
// Examples
//------------------------------//
int main() {
// 100,000 回サイコロを振って、それぞれの目が出る回数を数えてみる
int count[6] = {0};
for (int i = 0; i < 100000; ++i) {
int rng = rand_int() % 6;
count[rng]++;
}
for (int i = 0; i < 6; ++i) {
cout << i+1 << ": " << count[i] << " 回" << endl;
}
return 0;
}