-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.cpp
110 lines (92 loc) · 3.11 KB
/
generate.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
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
99
100
101
102
103
104
105
106
107
108
109
110
/**
* THIS CODE WAS TAKEN FROM https://piazza.com/class/lrkxtzhdnc831h/post/80.
* THIS IS CODE PROVIDED BY THE PROFESSOR, KENNETH CHIU, TO ASSIST WITH
* GENERATING TEST RESULTS FOR ASSIGNMENT 2.
*/
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <iomanip> // Required for std::setw and std::setfill
using namespace std;
class RandomGenerator {
public:
default_random_engine eng{random_device{}()};
uniform_int_distribution<int> str_len{10, 20};
uniform_int_distribution<int> char_dist{32, 126};
uniform_int_distribution<int> spaceDist{0, 10};
char getRandomChar() {
char randomChar;
do {
randomChar = static_cast<char>(char_dist(eng));
} while (randomChar == '\"' || randomChar == '.');
return randomChar;
}
string generateRandomString() {
int length = str_len(eng);
string str;
str.reserve(length);
for (int i = 0; i < length; ++i) {
char randomChar = getRandomChar();
if (randomChar == '\\') { // Ensure there's space for 2 characters
if (i >= length - 1) {
continue;
}
str += randomChar; // Add the first '\'
str += getRandomSpecialChar(); // Add the second character, which is either '\', '.', or '"'
i++; // Increment i since we've added two characters
} else {
str += randomChar;
}
}
return str;
}
const string &generateRandomSpace() {
static vector<string> vs {
"",
" ",
" ",
" ",
" ",
" "
};
int r = spaceDist(eng);
if (r >= vs.size()) {
return vs[0];
}
return vs[r];
}
private:
const string allowedChars = "\\\"";
uniform_int_distribution<int> specialCharDist{0, 1};
char getRandomSpecialChar() {
return allowedChars[specialCharDist(eng)];
}
};
int main(int argc, char const *argv[]) {
size_t n_line = 10;
if (argc >= 2) {
n_line = stoul(argv[1]);
}
const int numStrings = std::min<int>(n_line, 10000); // Use n_line instead of a fixed number
vector<string> randomStrings;
RandomGenerator randGen;
// Generate n_line random strings
for (int i = 0; i < numStrings; ++i) {
randomStrings.push_back(randGen.generateRandomString());
}
uniform_int_distribution<> intDist(0, 1000000000);
uniform_int_distribution<> strDist(0, numStrings - 1);
uniform_int_distribution<> zeroDist(1, 3);
for (size_t i = 0; i < n_line; ++i) {
int randomInt = intDist(randGen.eng);
int str_id = strDist(randGen.eng);
int r = zeroDist(randGen.eng);
if ( r == 1) {
cout << "\"" << randomStrings[str_id] << "\"" << randGen.generateRandomSpace() << std::setfill('0') << std::setw(11) << randomInt << endl;
} else {
cout << "\"" << randomStrings[str_id] << "\"" << randGen.generateRandomSpace() << randomInt << endl;
}
}
return 0;
}