@@ -22,9 +22,13 @@ int main() {
22
22
// This is embeded as a literal string for easy shipping with the binary.
23
23
// We could consider using some new Go feature to embed it as a static resource.
24
24
// At the time of creation of this, this is not a priority.
25
- const RandH = `
26
- #pragma once
25
+ const RandH = `#pragma once
26
+
27
+ #include <algorithm>
28
+ #include <cassert>
29
+ #include <climits>
27
30
#include <ctime>
31
+ #include <random>
28
32
#include <string>
29
33
30
34
const std::string LOWERCASE_ALPHABET = "abcdefghijklmnopqrstuvwyxz";
@@ -39,63 +43,71 @@ const int DIGITS = 1 << 2;
39
43
40
44
// Returns a random value of the following types
41
45
// int, long long, double, char, string
42
- // given main arguments
46
+ // given main arguments
43
47
// Example:
44
48
// Rand rand(argc, argv);
45
49
// int randomInt = rand.Int();
46
50
// string randomString = rand.String(10, DIGITS);
51
+ //
52
+ // @author NouemanKHAL
53
+ //
47
54
class Rand {
48
55
unsigned int seed;
49
56
50
- unsigned int ReadSeedFromArgs(int argc, char * argv[]) {
57
+ unsigned int ReadSeedFromArgs(int argc, char* argv[]) {
51
58
if (argc > 0) {
52
59
return std::stoi(argv[1]);
53
60
}
54
61
return 0;
55
62
}
56
63
57
- public:
64
+ public:
65
+ Rand(unsigned int _seed = 0) : seed(_seed) { srand(seed); }
58
66
59
- Rand(unsigned int _seed = 0) : seed(_seed) {
60
- srand(seed);
61
- }
62
-
63
- Rand(int argc, char * argv[]) {
67
+ Rand(int argc, char* argv[]) {
64
68
unsigned int seed = ReadSeedFromArgs(argc, argv);
65
- Rand{ seed };
69
+ Rand{seed};
66
70
}
67
71
68
72
// Returns an random int value in the range [from, to] inclusive
69
73
int Int(int from, int to) {
74
+ if (from == to) return from;
75
+ assert(from < to);
70
76
return rand() % (to - from) + from;
71
77
}
72
78
73
79
// Returns an random long long value in the range [from, to] inclusive
74
80
long long Long(long long from, long long to) {
81
+ if (from == to) return from;
82
+ assert(from < to);
75
83
return rand() % (to - from) + from;
76
84
}
77
85
78
86
// Returns an random double value in the range [from, to] inclusive
79
87
double Double(double from, double to) {
88
+ assert(from <= to);
80
89
double tmp = (double)rand() / RAND_MAX;
81
90
return from + tmp * (to - from);
82
91
}
83
92
84
93
// Returns an random char value in the range [from, to] inclusive
85
94
// Parameters are optional
86
95
char Char(char from = CHAR_MIN, char to = CHAR_MAX) {
87
- return static_cast <char> (Int(from, to));
96
+ assert(from <= to);
97
+ return static_cast<char>(Int(from, to));
88
98
}
89
99
90
100
// Returns an random char value in the range [from, to] inclusive
91
101
// Parameters are optional, by default returns a random lowercase letter
92
102
char Lower(char from = 'a', char to = 'z') {
103
+ assert('a' <= from && from <= to && to <= 'z');
93
104
return Char(from, to);
94
105
}
95
106
96
107
// Returns an random char value in the range [from, to] inclusive
97
108
// Parameters are optional, by default returns a random uppercase letter
98
109
char Upper(char from = 'A', char to = 'Z') {
110
+ assert('A' <= from && from <= to && to <= 'Z');
99
111
return Char(from, to);
100
112
}
101
113
@@ -107,8 +119,10 @@ public:
107
119
}
108
120
109
121
// Returns an random digit character
110
- // Parameters are optional, by default returns a random digit character in the range ['0', '9'] inclusive
122
+ // Parameters are optional, by default returns a random digit character in the
123
+ // range ['0', '9'] inclusive
111
124
char Digit(char from = '0', char to = '9') {
125
+ assert(from <= to);
112
126
return Char(from, to);
113
127
}
114
128
@@ -118,13 +132,13 @@ public:
118
132
return Digit();
119
133
}
120
134
135
+ // Returns a random boolean value.
136
+ bool Bool() { return bool(rand() & 1); }
121
137
122
- // Returns an std::string of length size consisting only of characters allowed in the given mask
123
- // using the constants LOWER, UPPER, DIGITS
124
- // Example:
125
- // Rand rand(argc, argv);
126
- // std::string str = rand.String(10, LOWER | DIGITS);
127
- // str is an std::string of size 10 consisting only of lowercase letters and digits
138
+ // Returns an std::string of length size consisting only of characters allowed
139
+ // in the given mask using the constants LOWER, UPPER, DIGITS Example: Rand
140
+ // rand(argc, argv); std::string str = rand.String(10, LOWER | DIGITS); str is
141
+ // an std::string of size 10 consisting only of lowercase letters and digits
128
142
std::string String(size_t size, const int mask = LOWER | UPPER) {
129
143
std::string charset;
130
144
@@ -144,7 +158,7 @@ public:
144
158
std::string res(size, ' ');
145
159
146
160
int len = charset.size();
147
-
161
+
148
162
for (char& c : res) {
149
163
size_t randomIndex = Int(0, len - 1);
150
164
c = charset[randomIndex];
@@ -192,8 +206,9 @@ int main() {
192
206
}
193
207
`
194
208
const JavaTemplate = `
195
- import java.util.*;
209
+
196
210
import java.io.*;
211
+ import java.util.*;
197
212
198
213
/**
199
214
* Made by egor https://github.com/chermehdi/egor.
@@ -202,17 +217,12 @@ import java.io.*;
202
217
* {{end}}
203
218
*/
204
219
public class Main {
205
-
206
- void solve(Scanner in, PrintWriter out) {
207
-
208
- }
209
-
210
- public static void main(String[] args) {
211
- try(Scanner in = new Scanner(System.in);
212
- PrintWriter out = new PrintWriter(System.out)) {
213
- new Main().solve(in, out);
214
- }
220
+ void solve(Scanner in, PrintWriter out) {}
221
+ public static void main(String[] args) {
222
+ try (Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out)) {
223
+ new Main().solve(in, out);
215
224
}
225
+ }
216
226
}
217
227
`
218
228
0 commit comments