-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (60 loc) · 1.91 KB
/
main.cpp
File metadata and controls
72 lines (60 loc) · 1.91 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
// Problem: Randomly distribute fixed number of objects into
// fixed number of buckets. Each bucket should atleast
// have an object.
//
// Solution Algorithm:
// 1) Put one object into each bucket.
// 2) For each remaining object put it
// into randomly selected bucket.
//
// Copyright (c) 2014 Pranav Kulkarni
// Copyright (c) 2014 Nishant Singh
// Copyright (c) 2014 Rohan Sharma
//
// See the file license.txt for copying permission.
//
#include <cmath>
#include <iostream>
#include <vector>
#include <random>
using namespace std;
// Get a random number from the range (inclusive)
int get_rand_from_range(double min, double max) {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(min, max+1);
return dist(gen);
}
// Computes the object distribution
vector<int> gen_object_distribution(int bucket_count, int object_count) {
vector<int> r(bucket_count, 1);
for(int iter = 0; iter < (object_count - bucket_count); ++iter) {
r[get_rand_from_range(0, bucket_count-1)]++;
}
return r;
}
int main(int argc, char *argv[]) {
int bucket_count = 10, object_count = 100;
int test_sum = 0;
vector<int> distribution;
if(argc < 3) {
cout << "Usage: " <<argv[0] << " <buckets> <objects>. Now using default values >"
<< " bucket_count : " << bucket_count << " object_count : " << object_count
<< "." << endl;
}
else {
string::size_type sz;
bucket_count = stoi(argv[1], &sz);
object_count = stoi(argv[2], &sz);
}
distribution = gen_object_distribution(bucket_count, object_count);
cout << "Distribution:" << endl;
for(vector<int>::size_type bid = 0; bid < distribution.size(); ++bid) {
cout <<"[Bucket " << (bid+1) << ":\t" << distribution[bid] << " objects]\n";
test_sum += distribution[bid];
}
cout << endl
<< ((test_sum == object_count) ? "Done!" : "Sucked!")
<< endl;
return 0;
}