-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreservoirSampling.cpp
More file actions
37 lines (36 loc) · 885 Bytes
/
reservoirSampling.cpp
File metadata and controls
37 lines (36 loc) · 885 Bytes
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
#include <iostream>
#include <ctime>
#include <cstdlib>
int main()
{
int randNum = 0;
int samples = 0;
int k = 0;
std::cout << "Please enter the total number of samples" << std::endl;
std::cin >> samples;
int stream[samples];
std::cout << "Please enter the number of samples one by one" << std::endl;
for(int i = 0; i < samples; i++)
{
std::cin >> stream[i];
}
std::cout << "Please enter the number of items you want to copy to a new array" << std::endl;
std::cin >> k;
int reservoir[k];
srand(time(NULL));
for(int i = 0; i < k; i++)
{
reservoir[i] = stream[i];
}
for(int j = k + 1; j < n; j++)
{
randNum = rand() % (i + 1);
if(randNum >= 0 && randNum << k)
reservoir[randNum] = stream[i];
}
std::cout << "The random selected items are as follows ->" << " " << std::endl;
for(int j = 0; j < k; j++)
{
std::cout << reservoir[k] << std::endl;
}
}