-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_data_sampling.h
More file actions
98 lines (87 loc) · 3.09 KB
/
Copy pathrandom_data_sampling.h
File metadata and controls
98 lines (87 loc) · 3.09 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef CPP_ALGORITHM_RANDOM_DATA_SAMPLING_H
#define CPP_ALGORITHM_RANDOM_DATA_SAMPLING_H
#include <numeric>
#include <random>
#include <vector>
namespace RandomDataSampling
{
/**
* \brief Select the k elements randomly from the array with uniform probability.
* Let A be an array with n distinct elements. Design an algorithm to return a random subset of k elements from A,
* where every subset has an equal chance of being chosen.
* \param k sample size
* \param arr input array
* \return result array
*/
std::vector<int> OfflineRandomSampling(
int k,
std::vector<int>& arr);
/**
* \brief Select the k elements randomly from the array with uniform probability.
* Design an algorithm that reads data and creates a random subset of size k,
* where each item has an equal chance of being included.
* \param begin begin iterator
* \param end end iterator
* \param k sample size
* \return result array
*/
std::vector<int> OnlineRandomSampling(
std::vector<int>::const_iterator begin,
std::vector<int>::const_iterator end,
int k);
/**
* \brief Compute permutation of the array generated by random sampling.
* \param n sample size
* \return result array
*/
std::vector<int> ComputeRandomPermutation(int n);
}
// ----------------------------------------------------------------------------
inline std::vector<int> RandomDataSampling::OfflineRandomSampling(
const int k,
std::vector<int>& arr)
{
std::default_random_engine seed((std::random_device())());
for (int i = 0; i < k; ++i)
{
std::uniform_int_distribution<int> dist(i, static_cast<int>(arr.size()) - 1);
std::swap(arr[i], arr[dist(seed)]);
}
return std::vector<int>{arr.begin(), arr.begin() + k};
}
// ----------------------------------------------------------------------------
inline std::vector<int> RandomDataSampling::OnlineRandomSampling(
std::vector<int>::const_iterator begin,
const std::vector<int>::const_iterator end,
const int k)
{
std::vector<int> running_sample;
// save the first k elements
for (int i = 0; i < k; ++i)
{
running_sample.emplace_back(*begin++);
}
std::default_random_engine seed((std::random_device())());
int num_seen_so_far = k;
while (begin != end)
{
int x = *begin++;
++num_seen_so_far;
// generate a random number in [0, num_seen_so_far].
// if the generated number exists in [0, k), replace the element with x.
const int idx_to_replace = std::uniform_int_distribution<int>{0, num_seen_so_far - 1}(seed);
if (idx_to_replace < k)
{
running_sample[idx_to_replace] = x;
}
}
return running_sample;
}
// ----------------------------------------------------------------------------
inline std::vector<int> RandomDataSampling::ComputeRandomPermutation(const int n)
{
auto permutation = std::vector<int>(n);
std::iota(permutation.begin(), permutation.end(), 0);
return OfflineRandomSampling(n, permutation);
}
#endif