Skip to content

Instantly share code, notes, and snippets.

@gitzhou
Last active October 14, 2015 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitzhou/3dc384a888bf985bf9ea to your computer and use it in GitHub Desktop.
Save gitzhou/3dc384a888bf985bf9ea to your computer and use it in GitHub Desktop.
随机-按权值-分发消息队列中的消息-消息队列内消息总数已知
//
// main.cpp
// director-random-distribute-via-weight-0
//
// 有 N 个消费者,分别有相应的权值
// 随机分发消息队列中的消息
// 各个消费者消费的消息数量的比值,总体近似为权值的比值
//
// Created by aaron67 on 15/10/13.
// Copyright © 2015年 aaron67. All rights reserved.
//
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
class Consumer {
public:
explicit Consumer(int weight = 0) : weight_(0) {}
void set_id(char id) {
id_ = id;
}
void set_weight(int weight) {
weight_ = weight;
}
inline int weight() const {
return weight_;
}
inline void consume(int message_id ) {
message_id_list_.push_back(message_id);
}
inline size_t consumed_messages_count() const {
return message_id_list_.size();
}
void display_consumed_messages() const {
const int MESSAGE_ID_COUNT_PER_LINE = 15;
cout << id_ << ": " << message_id_list_.size();
for (int i = 0; i < message_id_list_.size(); ++i) {
if (i % MESSAGE_ID_COUNT_PER_LINE == 0) {
cout << endl;
}
cout << message_id_list_[i] << ' ';
}
cout << endl;
}
private:
char id_;
int weight_;
vector<int> message_id_list_;
};
const int TOTAL_MESSAGE_COUNT = 200;
const int MAX_RAND_COUNT = 10;
void distribute(vector<Consumer> &consumers, int message_id) {
int total_weight = 0;
for (int i = 0; i < consumers.size(); ++i) {
total_weight += consumers[i].weight();
}
int rand_count = MAX_RAND_COUNT;
while (rand_count--) {
int index = rand() % consumers.size();
if (consumers[index].consumed_messages_count() < 1.0 * TOTAL_MESSAGE_COUNT * consumers[index].weight() / total_weight ||
rand_count == 0) {
consumers[index].consume(message_id);
break;
}
}
}
int main(int argc, const char * argv[]) {
srand((unsigned int)time(NULL));
int consumer_count;
cin >> consumer_count;
vector<Consumer> consumers(consumer_count);
for (int i = 0; i < consumer_count; ++i) {
int weight;
cin >> weight;
consumers[i].set_id('A' + i);
consumers[i].set_weight(weight);
}
for (int i = 0; i < TOTAL_MESSAGE_COUNT; ++i) {
distribute(consumers, i);
}
for (int i = 0; i < consumer_count; ++i) {
consumers[i].display_consumed_messages();
cout << endl;
}
return 0;
}
7 3 8 2 1 4 3 2
A: 28
3 11 18 45 49 67 69 79 86 89 90 95 108 109 110
111 113 114 115 116 119 122 125 128 129 132 135 186
B: 59
1 4 13 14 15 17 27 30 32 33 36 42 60 71 97
103 123 126 131 138 139 147 148 150 154 157 158 161 162 163
164 165 166 168 169 170 171 173 174 175 176 178 179 180 181
182 183 184 187 188 189 190 191 193 194 195 196 197 198
C: 19
10 29 34 55 58 59 62 63 70 85 91 93 100 105 107
112 117 124 199
D: 9
8 9 16 37 39 40 48 51 56
E: 36
6 7 28 31 35 43 44 46 47 53 68 82 94 98 99
104 106 118 120 121 130 140 141 142 143 144 145 146 151 152
153 155 156 159 160 192
F: 29
0 2 5 12 19 20 21 24 25 26 38 50 65 66 74
75 76 77 84 101 102 127 133 134 136 137 149 167 177
G: 20
22 23 41 52 54 57 61 64 72 73 78 80 81 83 87
88 92 96 172 185
Program ended with exit code: 0
7 3 8 2 1 4 3 2
A: 29
6 15 19 48 56 60 62 74 82 83 87 96 101 106 117
123 129 131 133 145 152 154 156 158 159 162 165 185 196
B: 56
8 9 10 12 16 23 26 30 31 35 36 40 44 69 79
81 88 90 99 102 104 105 113 114 122 126 130 132 135 140
141 146 147 149 151 155 163 164 167 169 171 177 178 182 183
184 186 187 188 189 190 191 192 195 198 199
C: 19
7 11 22 24 33 38 42 55 67 71 72 76 94 103 115
116 119 121 176
D: 13
1 25 34 43 45 46 50 57 63 180 181 193 197
E: 36
5 18 27 47 51 53 54 59 75 77 78 89 91 92 95
110 111 120 127 134 137 138 142 144 150 153 157 160 161 168
170 172 173 174 175 179
F: 27
0 3 4 17 28 29 39 49 52 58 68 73 80 85 86
93 100 108 112 118 124 125 128 136 139 143 148
G: 20
2 13 14 20 21 32 37 41 61 64 65 66 70 84 97
98 107 109 166 194
Program ended with exit code: 0
7 3 8 2 1 4 3 2
A: 30
1 15 18 22 24 27 29 41 48 49 59 61 71 74 75
77 79 80 92 94 99 102 113 114 118 125 130 159 188 194
B: 60
11 20 21 28 31 33 40 45 47 53 55 57 60 64 69
82 88 96 97 100 101 104 109 117 124 127 128 134 136 144
145 148 152 153 154 158 160 162 163 164 165 166 168 169 172
174 178 179 182 185 186 187 190 191 192 193 195 197 198 199
C: 19
25 39 65 66 72 73 90 91 105 106 110 111 123 129 133
137 141 143 189
D: 10
8 9 16 23 30 38 42 43 50 177
E: 35
4 7 14 34 36 44 51 54 63 67 68 78 86 108 116
119 120 132 135 142 149 155 156 157 161 167 170 171 173 175
176 180 181 183 184
F: 28
0 5 6 10 13 17 19 26 52 83 85 95 103 107 112
115 121 122 126 131 138 139 140 146 147 150 151 196
G: 18
2 3 12 32 35 37 46 56 58 62 70 76 81 84 87
89 93 98
Program ended with exit code: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment