-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_number.cpp
56 lines (48 loc) · 1.16 KB
/
random_number.cpp
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
#include <iostream>
#include <cstdlib> /* srand, rand */
#include <time.h> /* time */
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int total_STAs = atoi(argv[1]);
int maximum_backoff_slots = atoi(argv[2]);
int total_attempts = atoi(argv[3]);
int test_rand;
int no_repetition;
std::vector<int> backoff_slots;
backoff_slots.reserve(total_STAs);
for (int i = 0; i < total_attempts; i++) {
no_repetition = 1;
for (int j = 1; j <= total_STAs; j++) {
srand(i+j);
if (j == 1) {
backoff_slots.push_back(rand() % maximum_backoff_slots);
}
else {
test_rand = rand() % maximum_backoff_slots;
for (std::vector<int>::iterator it = backoff_slots.begin(); it != backoff_slots.end(); ++it) {
if (test_rand == *it){
no_repetition = 0;
break;
}
}
if (!no_repetition) {
backoff_slots.clear();
break;
}
else {
backoff_slots.push_back(test_rand);
}
}
}
if (no_repetition) {
cout << i << ":\t";
for (std::vector<int>::iterator it = backoff_slots.begin(); it != backoff_slots.end(); ++it) {
cout << *it << "\t";
}
cout << endl;
}
}
return 0;
}