-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
offline_sampling.cc
68 lines (60 loc) · 2.17 KB
/
offline_sampling.cc
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <algorithm>
#include <functional>
#include <iterator>
#include <random>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/random_sequence_checker.h"
#include "test_framework/timed_executor.h"
using std::bind;
using std::default_random_engine;
using std::random_device;
using std::swap;
using std::uniform_int_distribution;
using std::vector;
void RandomSampling(int k, vector<int>* A_ptr) {
vector<int>& A = *A_ptr;
default_random_engine seed((random_device())()); // Random num generator.
for (int i = 0; i < k; ++i) {
// Generate a random index in [i, size(A) - 1].
swap(A[i], A[uniform_int_distribution<int>{
i, static_cast<int>(A.size()) - 1}(seed)]);
}
}
bool RandomSamplingRunner(TimedExecutor& executor, int k, vector<int> A) {
using namespace test_framework;
vector<vector<int>> results;
executor.Run([&] {
for (int i = 0; i < 100000; ++i) {
RandomSampling(k, &A);
results.emplace_back(begin(A), begin(A) + k);
}
});
int total_possible_outcomes = BinomialCoefficient(A.size(), k);
sort(begin(A), end(A));
vector<vector<int>> combinations;
for (int i = 0; i < BinomialCoefficient(A.size(), k); ++i) {
combinations.emplace_back(ComputeCombinationIdx(A, A.size(), k, i));
}
vector<int> sequence;
for (vector<int> result : results) {
sort(begin(result), end(result));
sequence.emplace_back(
distance(begin(combinations),
find(begin(combinations), end(combinations), result)));
}
return CheckSequenceIsUniformlyRandom(sequence, total_possible_outcomes,
0.01);
}
void RandomSamplingWrapper(TimedExecutor& executor, int k,
const vector<int>& A) {
RunFuncWithRetries(
bind(RandomSamplingRunner, std::ref(executor), k, std::cref(A)));
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "k", "A"};
return GenericTestMain(args, "offline_sampling.cc", "offline_sampling.tsv",
&RandomSamplingWrapper, DefaultComparator{},
param_names);
}